I mean to ask about the preservation of string literals within a call to execve
family. From OSTEP Chp.4:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main (int argc, char *argv[]) {
...
int rc = fork();
...
if (rc == 0) {
char *myargs[3];
myargs[0] = strdup("wc");
myargs[1] = strdup("p3.c");
myargs[2] = NULL;
execvp(myargs[0], myargs);
}
I denote ...
as irrelevant to my question. I wanted to ask about the code here. As I understand it, C sets up myargs
to be presented as the char *argv[]
of the main function of wc
. I want to understand the concept to do with string literals. From my understanding, the C standard allows the mutability of the argv
. For example:
int main (int argc, char *argv[]) {
argv[0][2] = 'd';
}
However, I'm wondering how this can be guaranteed when these character arrays are fixed length. Additionally, why was strdup()
called? From my understanding, this will heap-allocate the string, allowing it to be mutated, so is this necessary when calling execvp()
or would:
myargs[0] = "wc";
be acceptable?
From OSTEP Chp.4:
The chapter in that link is labeled 5, not 4.
However, I'm wondering how this can be guaranteed when these character arrays are fixed length.
The C standard says the program may modify the contents of the strings pointed to by argv
. A string is a sequence of characters terminated by (and including) a null character. Therefore, the program may modify those characters. Thus, it is only guaranteed it can use the bytes that are in the string when the program starts. It is not guaranteed it can make the strings longer or use any bytes beyond the null character.
Additionally, why was
strdup()
called?
Because the author did not know or neglected the fact that it is not necessary.
is this necessary when calling
execvp()
…?
No, execvp
will take steps to ensure the strings passed to it are made available to the new program. The program calling execvp
does not need to make copies.
I mean to ask about the preservation of string literals within a call to
execve
family.
A string literal is a piece of source code. It is not a string in the resulting program. A string literal is a sequence of characters inside quote marks, optionally with a prefix u8
, u
, U
, or L
, as in "abc"
or u8"def"
. The string literal is literally those characters in the source code, "abc"
, including the quote marks. It is not the characters “abc” in the program that is created. When a program is compiled, the compiler creates the string that is represented by the string literal (in the computing model used by the C standard). The resulting string is merely a string; there is nothing particularly special about it compared to a string that did not come from a string literal, except the C standard does not guarantee it can be modified. Some people call the resulting strings string literals, but this is a slightly sloppy use of language.
Whether a string passed to execvp
came from a string literal or was constructed by the program in some way or was read from input or has automatic, static, or dynamically allocated storage duration is irrelevant to execvp
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With