I have been struggling with this problem for the last few hours, and it is one of the stranger problems I have encountered in my 3 years learning programming.
I am trying to create a longer, space-separated string by concatenating strings found in argv from stdin. This is the code I originally wrote:
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include "stringtable.h"
#include "strhash.h"
#include "auxlib.h"
int main (int argc, char **argv) {
int c;
char* filename;
while((c = getopt(argc, argv, "@:Dly")) != -1){
switch(c){
case '@':
printf("@ detected\n");
char* debugString = optarg;
int pos_in_input = optind;
while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
strcat(debugString, " ");
strcat(debugString, argv[pos_in_input]);
pos_in_input++;
if(argv[pos_in_input] == NULL){break;}
}
printf("%s\n", debugString);
break;
case 'D':
printf("D detected\n");
break;
case 'l':
printf("l detected\n");
break;
case 'y':
printf("y detected\n");
break;
default :
printf("default detected\n");
}
}
}
Most of the above is my unfinished options handling. The code segment of interest is in the switch statement under case : “@”. The while loop is supposed to be creating the debugString by concatenating the strings found in argv, stopping when a string begins with “-” or when the end of argv is reached. I am also adding spaces between the strings with “strcat(debugString, ” “);” It is adding spaces that is giving me trouble.
If I concatenate all the strings without adding spaces in this way:
while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
strcat(debugString, argv[pos_in_input]);
pos_in_input++;
if(argv[pos_in_input] == NULL){break;}
}
I get the following:
Input: -@what is going on here?
Output: @ detected
whatisgoingonhere?
This is how I expected it to work. However, if I run the code that adds spaces:
while(argv[pos_in_input][0] != '-' && argv[pos_in_input] != NULL){
strcat(debugString, " ");
strcat(debugString, argv[pos_in_input]);
pos_in_input++;
if(argv[pos_in_input] == NULL){break;}
}
Then I get the following as output:
Input: -@what is going on here?
Output: @ detected
what going on here?
Notice the two spaces between ‘what’ and ‘going’. My program is still correctly adding spaces, even though for some reason a word is getting deleted. I have tried this with many different inputs, it is always the second string in input that gets deleted. Anyone know what is going on?
P.S. Compiling with gcc -g -O0 -Wall -Wextra -std=gnu99 -lm
None of the include files that I created are used in this piece of code, so the problem is not something in my includes.
Your code of interest starts with:
This makes the
debugStringpointer point to the same place thatoptargpoints to. This does not allocate a “new” string in any way. By callingstrcat(debugString, ...), you are overwriting whatever else happens to appear in memory after the end ofoptarg. Chaos will ensue.To address this problem, try:
This will allocate 1000 bytes for you to stuff bits of string into using
strcat. It is your responsibility to ensure that you do not write past the end of thedebugStringbuffer (so if you find you have more than 1000 bytes to concatenate, you’ll have to decide what you want to do with that).