For the php section, my sprintf is not working correctly to substitute the query string and is causing a "PHP Notice: Undefined index: name...". The path substitutes in just fine. It is rather baffling to me why this is happening because this was code that was provided, and by everything that I can see sprintf is being used correctly. Anyway, here's the code in question that does not work:

// open pipe to PHP interpreter

char* format = "QUERY_STRING=\"%s\" REDIRECT_STATUS=200 SCRIPT_FILENAME=\"%s\" php-cgi";

printf("query string: %s\n", query); //DEBUG

char command[strlen(format) + (strlen(path) - 2) + (strlen(query) - 2) + 1];

sprintf(command, format, query, path);

printf("command = %s\n",command); //DEBUG


query string: name=dog


SCRIPT_FILENAME="/home/jharvard/Dropbox/pset6/public/hello.php" php-cgi

PHP Notice: Undefined index: name in ....

If I hard-code the query string in the format string, it works just fine. Increasing the size of command doesn't help. Can anyone help me with this?

1 Answer 1


I solved this myself. The problem, of course, had to do with incorrect pointer use. I had created a fcn to parse out the query string. I parsed the query string into a local variable, and then set a main pointer variable to the local variable in the function--pretty stupid, huh? When program execution returned to main, the "old" variable had not yet been overwritten so it showed in gdb and printf. But apparently something about sprintf overwrites that expired local variable memory space so that the pointer to the query string passed to sprintf ends up with no value at all. Every time that I think I finally understand how to use pointers, I make a mistake like this and find that I really didn't. Hopefully this is the last lesson for me to learn.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .