【问题标题】:Changing pointer char value in function在函数中更改指针 char 值
【发布时间】:2015-12-09 14:18:48
【问题描述】:

我尝试解析客户端 HTML GET 请求并获取他想要发送的文件地址。但是当我将参数传递给路径变量中的函数时输出错误。 解析器函数 printf:New file path returned is=/somedir/index.html 主函数printf:In main path=random chars

怎么了?

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 ...
.
.
}

主要

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);
}

【问题讨论】:

  • 这一行:while(buffer[index] != ' '){ 在遇到空格时退出循环,而不是在遇到字符串末尾的 NUL 字节时。然后这一行: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);` 填充了 flePath[] 数组(不包括 NUL 字符串终止字符)。然后这一行:strcat(fullPath,name); 在 filePath[] 数组末尾的“某处”添加了更多字符。 ==未定义的行为
  • 您错误地使用了strncpy - 查看其手册了解原因

标签: c pointers char


【解决方案1】:
char filePath[numberOfChars];    
char* fullPath;
fullPath = filePath; 
....
(*newPath) = fullPath;

filePath 具有自动存储期限,您在其生命周期结束后访问它。这是undefined behaviour

相反,您可以使用malloc()strcpy() 将字符串复制到*newPath

替换

(*newPath) = fullPath;

与:

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

并在main() 中调用free() 以解除分配。


如 cmets 中所述,您需要为 '\0' 终止符分配一个额外的字节。你可以通过分配一个额外的字节来处理这个问题:

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

strncpy() 自动用 NUL 字节填充内存的其余部分。在这种情况下,只是最后一个字节。一般来说,您不想使用strncpy(),因为没有必要用 NUL 字节填充缓冲区的其余部分。

【讨论】:

  • filePath[] 数组未正确终止,因此对strlen() 的调用将无法返回正确的值。
  • @user3629249 谢谢。我已经更新以涵盖这一点。希望现在好多了。
【解决方案2】:

您正在返回一个指向局部变量的指针。当函数退出时,该变量超出范围,它占用的内存可以用于其他目的,导致undefined behavior

您的函数需要为字符串(和 NULL 终止符)动态分配空间并返回指向该缓冲区的指针。然后你需要确保在调用函数中free它:

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);
}

【讨论】:

  • 谢谢,但@l3x 是第一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2015-04-29
相关资源
最近更新 更多