I want the following code, which is related to the Sysbench tool (https://github.com/akopytov/sysbench), to change from the following code to the next code, but I get an error with just this small change (Segmentation fault (core dumped)).
./sysbench cpu --cpu-max-prime=2000 run
int main(int argc,char *argv[])
{
.
.
.
}
to
int main(void)
{
char *argv[]= {"./sysbench","cpu","--cpu-max-prime=2000","run", NULL};
int argc = sizeof(argv) / sizeof(char*) - 1;
.
.
.
}
with GDB Debug :
Program received signal SIGSEGV, Segmentation fault.
0x000000000040aec8 in parse_option (
name=name@entry=0x4768e8 "cpu-max-prime=2000",
ignore_unknown=ignore_unknown@entry=false) at sysbench.c:500
500 *tmp = '\0';
static int parse_option(char *name, bool ignore_unknown)
{
const char *value;
char *tmp;
option_t *opt;
char ctmp = 0;
int rc;
tmp = strchr(name, '=');
printf( "tmp: %s\n", tmp );
if (tmp != NULL)
{
ctmp = *tmp;
*tmp = '\0';
value = tmp 1;
}
else
{
value = NULL;
}
opt = sb_find_option(name);
if (opt != NULL || ignore_unknown)
rc = set_option(name, value,
opt != NULL ? opt->type : SB_ARG_TYPE_STRING) == NULL;
else
rc = 1;
if (tmp != NULL)
*tmp = ctmp;
return rc;
}
please help me. thanks.
CodePudding user response:
In order to emulate argv
from main
the strings pointed to by argv[n]
to must be writable, which is not the case in your code where argv[n]
point to string literals which cannot be written to.
Writing into a string literal is formally undefined behaviour in C, but on modern desktop platforms it typically triggers a seg fault.
And *tmp = '\0';
is actually writing into a string.
This should do the job, although I'm not entirely sure because I can't check it here easily.
int main(void)
{
char arg1[] = "./sysbench";
char arg2[] = "cpu";
char arg3[] = "--cpu-max-prime=2000";
char arg4[] = "run";
char* argv[] = {arg1, arg2, arg3, arg4, NULL};
...
}