2

I try to parse client HTML GET request and get address of file he wants to send. But when i pass argument to function in path variable is bad output. Parsers function printf: New file path returned is=/somedir/index.html Main fuction printf: In main path=random chars

What is wrong?

int parser(char* buffer,char **newPath)
{
int numberOfChars = 0;
int index = 4;//start at 4 char

while(buffer[index] != ' '){
            numberOfChars++;
            index++;
}
// in numberOfChars is number of path characters from while loop
// this part of code is in if statment but it is irrelevant now
char filePath[numberOfChars];    
strncpy(filePath,buffer+4,numberOfChars);
char* fullPath;
fullPath = filePath;                
char name[] = "index.html";
strcat(fullPath,name);

(*newPath) = fullPath;
printf("New file path returned is=%s\n",(*newPath));
return 1;
//some more code if file is .sh or .bash or .png ...
.
.
}

main

int main(int argc, char *argv[])
{
            //some code
            .
            .
            .
            char* path;
            //msg is client HTML get request i want parse it and send data
            // which he wants
            parser(msg,&path);
            printf("In main path=%s\n",path);
}
2
  • this line: while(buffer[index] != ' '){ exits the loop when encountering a space, not when encountering the NUL byte at the end of the string. Then this line: char filePath[numberOfChars]; 1) does not allow room for trailing NUL char. 2) is too small to hold the buffer[5] through the end of the char array. Then this line: strncpy(filePath,buffer+4,numberOfChars);` filled up the flePath[] array (without including a NUL string termination char). Then this line: strcat(fullPath,name); adds yet more characters, 'somewhere' beyond the end of the filePath[] array. ==undefined behaviour Commented Dec 10, 2015 at 19:37
  • You are using strncpy incorrectly - check its manual to see why Commented Dec 10, 2015 at 20:26

2 Answers 2

1
char filePath[numberOfChars];    
char* fullPath;
fullPath = filePath; 
....
(*newPath) = fullPath;

filePath has automatic storage duration and you are accessing it after its lifetime is over. This is undefined behaviour.

Instead, you could use malloc() and strcpy() to copy the string into *newPath.

Replace

(*newPath) = fullPath;

with:

*newPath = malloc(strlen(fullPath) + 1);
 if (*newPath == NULL) {
    /* handle error */
 }
strcpy(*newPath, fullPath);

and call free() in main() to de-allocate it.


As noted in the comments, you need to allocate one extra byte for the '\0' terminator. You can handle this by allocating an extra byte:

char filePath[numberOfChars + 1];    
strncpy(filePath,buffer+4,numberOfChars);
filePath[numberOfChars] = '\0';

strncpy() automatically fills rest of the with the memory with NUL bytes. In this case, just the last byte. In general, you don't want to use strncpy() as that would be unnecessary to fill the rest of the buffer with NUL bytes.

Sign up to request clarification or add additional context in comments.

2 Comments

the filePath[] array has not been properly terminated, so the call to strlen() will fail to return the correct value.
@user3629249 Thanks. I have updated to cover this. Hopefully, it's better now.
1

You're returning a pointer to a local variable. When the function exits, that variable goes out of scope and the memory it occupied can be used for other purposes, leading to undefined behavior.

Your function needs to dynamically allocate space for the string (and the NULL terminator) and return a pointer to that buffer. You then need to be sure to free it in the calling function:

int parser(char* buffer,char **newPath) {
    ....
    char *filePath = malloc(numberOfChars+1);
    if (filePath == NULL) {
        perror("malloc failed");
        exit(1);
    }
    ...
}

int main(int argc, char *argv[])
{ 
    ...
    parser(msg,&path);
    printf("In main path=%s\n",path);
    free(path);
}

1 Comment

Thank you but @l3x was first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.