memset 无法将字符串设置为零并进入分段错误

memset fails to set a string to zero and goes into segmentation fault

我正在做一个关于 C 中的 UDP 套接字的练习。当客户端发送特定消息(例如 hi)时,服务器必须发送 "Nice to meet you"。如果没有找到标准回复,服务器将发送 "No suitable reply"。我的问题是,如果我尝试 return 这样的回复,memset 会失败:

return "No suitable reply";

如果我return这样回复就不会了:

char* foo = malloc(sizeof(char*));
memset(foo, 0, strlen(ses));
memcpy(foo, "No suitable reply", 17);
return foo;

我尝试 google 解决这个问题,我发现 this and this,但他们似乎没有解决我的问题(我首先认为 memset 不适用于声明的字符串像 char string[] = "something" 但在第二个示例中,他们在静态字符串上使用 memset)。

这是完整的代码(我说的memset就在最后):

   /*
    Alessandro Dussin 5AI
    2018-17-11
    Write a program to handle a single UDP "connection"
    */

//Standard libraries
#include <stdio.h>
#include <stdlib.h>

//Sockets libraries and connection ahndling
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>

//Read/write ops on file descriptors
#include <unistd.h>
//String ops
#include <string.h>

#include <assert.h>
void chopN(char *str, size_t n)
{
    assert(n != 0 && str != 0);
    size_t len = strlen(str);
    if (n > len)
        return;  // Or: n = len;
    memmove(str, str+n, len - n + 1);
}

//Required by the exercise. Given a certain word or phrase, reply with a specific string
char* switchreply(char* str){
    //Extracts the word or phrase (Basically removes the "/command " word)
    chopN(str, strlen("/stdreply "));
    int i = 0;
    for(; i < strlen(str); i++){
        if(str[i] == '\n'){
            str[i] = '[=12=]';
            break;
        }
    }
    if(strcmp(str, "ciao") == 0){
        return "ciao anche a te!";
    }
    else if(strcmp(str, "I hate you") == 0){
        return "I hate you too!";
    }
    return "";
}


char* stdreply(char *str){

    char* tmp = malloc(sizeof(char)*128);
    int i = 0;
    //printf("Entered stdreply... str at the start of the func: %s\n", str);
    for(; i < strlen(str); i++){
        tmp[i] = str[i];
        //printf("tmp: %s\n", tmp); //DEBUG
        if(strcmp(tmp, "/echo ") == 0){ // if(strcmp() == 0) is necessary because
                                        //otherwise 0 would be interpreted as FALSE
            //printf("Echo detected\n"); //DEBUG
            chopN(str, strlen("/echo "));
            str[strlen(str)] = '[=12=]';
            return str;
        }
        else if(strcmp(tmp, "/stdreply ") == 0){
            //printf("I got into the else if\n"); //DEBUG
            char* tmpreply = calloc(strlen(str), sizeof(char*));
            tmpreply = switchreply(str);
            //printf("tmpreply: %s\n", tmpreply);
            str = malloc(sizeof(char*)*strlen(tmpreply));
            memcpy(str, tmpreply, strlen(tmpreply));
            //str[strlen(str)] = '[=12=]'; //DEBUG
            //printf("str: %s\n", str); //DEBUG
            return str;
        }
        else if(strcmp(tmp, "/TODO") == 0){
            char* ses = malloc(sizeof(char*));
            memset(ses, 0, strlen(ses));
            memcpy(ses, "work in progress", 17);
            return ses;
        }

    }
    return "No suitable reply";

    }

    int main(int argc, char **argv){

    if(argc < 2){
        printf("Usage: ./server port");
        exit(0);
    }

    int serverfd;
    serverfd = socket(AF_INET, SOCK_DGRAM, 0);

    struct sockaddr_in server;
    server.sin_port = htons(atoi(argv[1]));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;

    if(bind(serverfd, (struct sockaddr *)&server, sizeof(server)) < 0){
        perror("Bind() error: ");
        fflush(stderr);
    }

    //"UDP message receiver" variables declarations
    int bytes; //Reads how many bytes the funcion recvfrom has read
    struct sockaddr_in from;

    char* buffer = malloc(sizeof(char*)); //String to which save the client message
    memset(buffer, 0, strlen(buffer)); //and set it to zero

    socklen_t fromlen = sizeof(struct sockaddr_in);

    const char stdrep[] = "Message Received: "; //This string will always be
                                                //printed upon receiving a message
    char* reply = malloc(sizeof(char*)); //This is where the return value of
                                        //stdreply() will be stored
    memset(reply, 0, strlen(reply)); //and set it zero

    //This while will keep "listening" for udp messages
    while((bytes = recvfrom(serverfd, buffer, 1024, 0, (struct sockaddr *)&from, &fromlen)) > 0){
        //From teacher's example. Write to stdout
        write(1, stdrep, strlen(stdrep));
        write(1, buffer, bytes);
        //Detect a basically empty string (if the client has pressed only enter)
        if(buffer[0] == '\n'){
            bytes = sendto(serverfd, "You pressed only enter!\n", 18, 0, (struct sockaddr *)&from, fromlen);
        }

        //Act according to the client message
        reply = stdreply(buffer);
        bytes = sendto(serverfd, reply, strlen(reply), 0, (struct sockaddr *)&from, fromlen);
        if (bytes  < 0){
            perror("sendto: ");
            fflush(stderr);
        }
        memset(buffer, 0, 1024);
        memset(reply, 0, strlen(reply)); //The seg fault happens right here
        fflush(stdout);
    }
    return 0;
}
reply = stdreply(buffer);

这不会复制字符串。它用不同的指针覆盖指针,丢失原始指针。

memset(reply, 0, strlen(reply));

如果字符串是用 malloc 分配的,这将清除该字符串。如果它是像 "No suitable reply" 这样的常量字符串,那么它可能是只读的,因此会产生段错误。

您发布的代码中有很多问题。

  1. 正如@JonBolinger 已经指出的那样,sizeof(char*) returns 指向 char 的指针的大小(以字节为单位)。在 Intel 平台上,这将是 4 或 8,具体取决于您是 运行 在 32 位还是 64 位上。 (所以你最终分配了 4 或 8 个字节的缓冲区)

  2. 您一直尝试使用 memset() 清除动态分配的缓冲区。 malloc() 将 return 内存充满垃圾,您可以通过在 returned 缓冲区上使用 strlen() 来指示要清除多少字节。 strlen() 将扫描缓冲区,直到找到第一个 0 字符以计算字符串的长度。 由于缓冲区中充满了垃圾,这很容易给你一个超出内存块边界的值,你最终会破坏内存。

  3. 每个对 malloc() 的调用都应该与 free() 调用相匹配,否则会泄漏内存。如果您的程序很长-运行,这一点尤其重要。

当您使用临时本地字符串(未 return 发送给调用者的字符串)时,使用本地字符数组而不是 malloc() 是很常见的做法。这样,缓冲区在堆栈上分配,并在您的函数退出作用域时自动释放。一定要使用 'safe' 字符串函数,例如 strncpy(),它将接收缓冲区的长度作为参数,以避免覆盖。

void Example(char* anotherString ) {
    char tmpString[256];   // this will create a local buffer with capacity of 256 bytes
    strncpy(tmpString, anotherString, sizeof(tmpString));  // copy string, without risk of overflowing the buffer
}

警告:从不 尝试 return 本地临时缓冲区作为结果,请记住当函数退出时它将不再存在,尽管 returned 值最初可能有有意义的结果,一旦您调用另一个函数,它们肯定会被销毁。取而代之的是另一种常见的做法,当您需要一个字符串 return 值时,而不是 returning 用 malloc() 分配的字符串 - 需要用 free() 释放 -您传递一个将结果作为参数保存的本地缓冲区,如下所示:

void func1() {
    char result[256];
    func2(result, 256);
    // after calling, result will carry "a returned string"
}

void func2(char* result, size_t bufferLen) {
    strncpy(result, "a returned string", bufferLen);
}

我认为如果您可以将代码转换为在适用的情况下使用这种样式,您的代码将会受益匪浅。