Hi! I'm constructing a code for parsing results in an html form and I'm having problems with strcat. It's really weird 'cause I'm positive the syntax and all that are all correct. I can't seem to find where the problem is.
Basically, I have this non-empty string "copy" and when I use:

strcat(copy,"test");

A server error occurs.
(test is just a test string to confirm if what I'm doing is correct)
But when I put the same code in a different part which should have no difference, the code is implemented correctly.

Here is the part of the code I'm having problems with:
(sorry if the code is kinda lousy)

if (data){

//MISCELLANEOUS CODE HERE

        int check1 = 0,check2 = 0,check3 = 0;
        char *copy = NULL,addressholder2;

        copy = &addressholder2;
        *copy = *string; //string is the result of the html form
        copy[1] = '\0';
        strcat(copy,"test"); //HERE IS THE CODE THAT WORKS

        do{
            //PARSES FIRST CHECKBOX RESULT
            if (strncmp(string,"&Birthday wish=",strlen("&Birthday wish="))==0){
                if (check1==0){
                    printf("<br>Birthday wish=");
                    check1++;

                    //strncat(copy,"Birthday wish=",14);
                }
                else if (check1!=0){
                    printf(",");
                    //strncat(copy,",",1);              
                }

                string = string + strlen("&Birthday wish=") - 1;    
            }
            //PARSES SECOND CHECKBOX RESULT
            else if (strncmp(string,"&Food=",strlen("&Food="))==0){
                if (check2==0){
                    printf("<br>Food=");
                    check2++;

                    //strncat(copy,"Food=",5);
                }
                else if (check2!=0){
                    printf(",");
                    //strncat(copy,",",1);
                }

                string = string + strlen("&Food=") - 1;
            }
            //PARSES THIRD CHECKBOX RESULT
            else if (strncmp(string,"&Drinks=",strlen("&Drinks="))==0){
                if (check3==0){
                    printf("<br>Drinks=");
                    check3++;

                    //strncat(copy,"Drinks=",7);
                }
                else if (check3!=0){
                    printf(",");
                    //strncat(copy,",",1);
                }

                string = string + strlen("&Drinks=") - 1;
            }
            //REPLACE '&' WITH NEWLINE
            else if (*string=='&'){
                printf("<br>");
                //strcat(copy,"\n");
            }
            else{
                printf("%c",*string);
                //strcat(copy,"test"); //HERE IS ONE OF THE LINES THAT DOESN'T WORK
            }

            string++;
        }while(*string!='\0');

Basically if one of the strcat are not comments anymore, a server error occurs. I'm positive my algorithm here is correct, or have I missed something? I uploaded my .html file if you want to test it yourself. Thanks :)
The whole code is:

if (data){

    //MISCELLANEOUS CODE HERE

            int check1 = 0,check2 = 0,check3 = 0;
            char *copy = NULL,addressholder2;

            copy = &addressholder2;
            *copy = *string; //string is the result of the html form
            copy[1] = '\0';
            strcat(copy,"test"); //HERE IS THE CODE THAT WORKS

            do{
                //PARSES FIRST CHECKBOX RESULT
                if (strncmp(string,"&Birthday wish=",strlen("&Birthday wish="))==0){
                    if (check1==0){
                        printf("<br>Birthday wish=");
                        check1++;

                        //strncat(copy,"Birthday wish=",14);
                    }
                    else if (check1!=0){
                        printf(",");
                        //strncat(copy,",",1);              
                    }

                    string = string + strlen("&Birthday wish=") - 1;    
                }
                //PARSES SECOND CHECKBOX RESULT
                else if (strncmp(string,"&Food=",strlen("&Food="))==0){
                    if (check2==0){
                        printf("<br>Food=");
                        check2++;

                        //strncat(copy,"Food=",5);
                    }
                    else if (check2!=0){
                        printf(",");
                        //strncat(copy,",",1);
                    }

                    string = string + strlen("&Food=") - 1;
                }
                //PARSES THIRD CHECKBOX RESULT
                else if (strncmp(string,"&Drinks=",strlen("&Drinks="))==0){
                    if (check3==0){
                        printf("<br>Drinks=");
                        check3++;

                        //strncat(copy,"Drinks=",7);
                    }
                    else if (check3!=0){
                        printf(",");
                        //strncat(copy,",",1);
                    }

                    string = string + strlen("&Drinks=") - 1;
                }
                //REPLACE '&' WITH NEWLINE
                else if (*string=='&'){
                    printf("<br>");
                    //strcat(copy,"\n");
                }
                else{
                    printf("%c",*string);
                    //strcat(copy,"test"); //HERE IS THE CODE THAT DOESN'T WORK
                }

                string++;
            }while(*string!='\0');

    }
    printf("</body></html>");

    return 0;
}
Attachments
<html>
	<body bgcolor=black>
	<form action=cgi-bin/get.cgi method=get>
		<font color=yellow>
			<center><h1>Please complete the survey below:</h1></center>

			<h2>User information:</h2>

			Name: <input type=text name=Name><br>
			Age: <input type=text name=Age><br>
			Gender:
			<input type=radio value='Male' name=Gender>Male
			<input type=radio value='Female' name=Gender>Female<br>
			Birthdate:
			<input type=text name=Birthdate><br><br>

			<h2>What do you want for your birthday?</h2>

			<input type=checkbox value='clothes' name=Birthday+wish>new clothes<br>		
			<input type=checkbox value='books' name=Birthday+wish>books<br>
			<input type=checkbox value='food' name=Birthday+wish>food<br>
			<input type=checkbox value='pet' name=Birthday+wish>a pet<br>
			<input type=checkbox value='others' name=Birthday+wish>Others<br>

			<h2>Do you want to have a birthday party/celebration?</h2>

			<input type=radio value='yes' name=Party>Yes<br>
			<input type=radio value='no' name=Party>No<br>
			<br>
			<h3>If yes, please fill up the remaining questions:</h3>

			Venue:
			<select name=Venue>
				<option value='None' selected>-Select venue-</option>
				<option value='home'>Home</option>
				<option  value="relative's+house">Relative's house</option>
				<option  value='school'>School</option>
				<option  value='0ffice'>Office</option>
				<option  value='restaurant'>Restaurant</option>
				<option  value='unspecified'>None of the above</option>
			</select><br><br>

			Food (in general):<br>
			<input type=checkbox value='chicken' name=Food>Chicken<br>
			<input type=checkbox value='beef' name=Food>Beef<br>
			<input type=checkbox value='pork' name=Food>Pork<br>
			<input type=checkbox value='noodles' name=Food>Noodles<br>
			<input type=checkbox value='sandwhiches' name=Food>Sandwhiches<br>
			<input type=checkbox value='soup' name=Food>Soup<br>
			<input type=checkbox value='etc.' name=Food>Others<br>

			Please give more details (serving,kind of dish,etc.) about the food you want:<br>
			(e.g. I want fried chicken, noodles except for carbonara. I also like pork adobo, not grilled or fried.)<br>			
			<textarea rows=5 cols=100 name=Food+details></textarea><br><br>

			Drinks (in general):<br>
			<input type=checkbox value='juice' name=Drinks>Juice<br>
			<input type=checkbox value='softdrinks' name=Drinks>Softdrinks<br>
			<input type=checkbox value="iced+tea" name=Drinks>Iced Tea<br>
			<input type=checkbox value='etc.' name=Drinks>Others<br>

			If you want to put in more details about your birthday party, you can upload a file here specifying those details<br>
			(posters/images,invited guests,invitations,etc.)<br>
			<input type=file name=Party+details><br>	
		</font>	
		<br><input type=submit value=Submit>
	</form>
	</body>
</html>

char *copy = NULL,addressholder2;
copy = &addressholder2;
*copy = *string; //string is the result of the html form
copy[1] = '\0';
strcat(copy,"test"); //HERE IS THE CODE THAT WORKS

This is terrifying. So you have a pointer to a single character, and then immediately start writing beyond that single character as if it were an array to infinite memory. This code exhibits a profound misunderstanding of how pointers and memory work in C.

Assuming that you haven't done something equally wrong with string, it looks like you can use the length of string as a size for copy because it's pretty much guaranteed to be more than enough:

char *copy = malloc(strlen(string) + 1);

if (copy) {
    /* This may be overly paranoid, but handle an empty string */
    if (string[0] != '\0') {
        copy[0] = string[0];
        copy[1] = '\0';
    }
    else {
        copy[0] = '\0';
    }

    /* Now do all of your stuff */
    strcat(copy, "test");

    ...
}

Also, don't forget to release the memory for copy by calling free() at a suitable location (ie. when you're done using the memory).

Oh I get it. That's why I can't seem to find an error because I never realized that the pointers could have a conflict in memory. Thanks a lot! :D Now I can move forward with what I'm doing :) Oh, one last question. If "copy" has, say, "Name" and I were to do this:

strcat(copy,"\nAge");

Would "\n" be stored inside the same address so that it still functions as a new line or not since I'm using this as a .cgi file connected to an html file and only <br> works? Because I have to save "copy" in a text file. Thanks for your help! :)

Edited 4 Years Ago by aldrin_ison1

Would "\n" be stored inside the same address so that it still functions as a new line or not since I'm using this as a .cgi file connected to an html file and only <br> works? Because I have to save "copy" in a text file.

If it's going into a text file then '\n' will act as a newline. If it's being rendered by a browser, I'm not 100% sure whether the CGI interpreter will convert that to an HTML line break or not. That's something you'd need to test and adjust accordingly. I actually haven't worked with CGI in about 10 years, so the details are fuzzy. ;)

Edited 4 Years Ago by deceptikon

This article has been dead for over six months. Start a new discussion instead.