c 动态分配断言失败

发布于 2024-11-10 14:44:51 字数 3165 浏览 2 评论 0原文

我在运行时运行使用此函数的程序时遇到一些问题,

static
int upload_build_buffer(char **upload_buffer,int seq_n, char* buffer) {

    char *up_buffer = NULL;
    assert(buffer);
    assert(upload_buffer);

    *upload_buffer = NULL;
    *upload_buffer = malloc((SIZE_SEQ_N+strlen(buffer)+1) * sizeof(char));
    if(!(*upload_buffer))
        return 1;
    up_buffer = malloc((SIZE_SEQ_N+strlen(buffer)+1) * sizeof(char));
    if(!(up_buffer))
        return 1;

    /* prints formatted sequence number in its string */
    snprintf((*upload_buffer), SIZE_SEQ_N+1, "%5d", seq_n);
    (*upload_buffer)[SIZE_SEQ_N] = 0;


    /* creates payload -> buffer = seq_number + buffer */
    strncpy(up_buffer,strncat((*upload_buffer), buffer, SIZE_PAYLOAD), SIZE_SEQ_N + SIZE_PAYLOAD);

    free(*upload_buffer);
    *upload_buffer = up_buffer;
    return 0;
}

如果多次执行此函数,我会收到此错误:函数

malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

的调用如下:

/* builds buffer to send */
if(seq_numbered_buffer) {
    free(seq_numbered_buffer);
    seq_numbered_buffer = NULL;
}
if(upload_build_buffer(&seq_numbered_buffer, seq_n, buffer))
    return 1;

此外,如果我运行 valgrind 我得到:

==2832== Invalid write of size 1
==2832==    at 0x4026FBF: strncpy (mc_replace_strmem.c:339)
==2832==    by 0x80490F7: upload_build_buffer (uftp_client.c:189)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)
==2832==  Address 0x419b0c4 is 0 bytes after a block of size 52 alloc'd  
==2832==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2832==    by 0x8049062: upload_build_buffer (uftp_client.c:174)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)
==2832== 
==2832== Invalid write of size 1
==2832==    at 0x4026FCB: strncpy (mc_replace_strmem.c:339)
==2832==    by 0x80490F7: upload_build_buffer (uftp_client.c:189)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)
==2832==  Address 0x419b0c5 is 1 bytes after a block of size 52 alloc'd
==2832==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2832==    by 0x8049062: upload_build_buffer (uftp_client.c:174)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)

我不知道如何调试此代码..

I have some trouble running a program that uses this function

static
int upload_build_buffer(char **upload_buffer,int seq_n, char* buffer) {

    char *up_buffer = NULL;
    assert(buffer);
    assert(upload_buffer);

    *upload_buffer = NULL;
    *upload_buffer = malloc((SIZE_SEQ_N+strlen(buffer)+1) * sizeof(char));
    if(!(*upload_buffer))
        return 1;
    up_buffer = malloc((SIZE_SEQ_N+strlen(buffer)+1) * sizeof(char));
    if(!(up_buffer))
        return 1;

    /* prints formatted sequence number in its string */
    snprintf((*upload_buffer), SIZE_SEQ_N+1, "%5d", seq_n);
    (*upload_buffer)[SIZE_SEQ_N] = 0;


    /* creates payload -> buffer = seq_number + buffer */
    strncpy(up_buffer,strncat((*upload_buffer), buffer, SIZE_PAYLOAD), SIZE_SEQ_N + SIZE_PAYLOAD);

    free(*upload_buffer);
    *upload_buffer = up_buffer;
    return 0;
}

at runtime I get this error if I execute this function more than once:

malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.

the call of function is the following:

/* builds buffer to send */
if(seq_numbered_buffer) {
    free(seq_numbered_buffer);
    seq_numbered_buffer = NULL;
}
if(upload_build_buffer(&seq_numbered_buffer, seq_n, buffer))
    return 1;

moreover if I run valgrind I get:

==2832== Invalid write of size 1
==2832==    at 0x4026FBF: strncpy (mc_replace_strmem.c:339)
==2832==    by 0x80490F7: upload_build_buffer (uftp_client.c:189)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)
==2832==  Address 0x419b0c4 is 0 bytes after a block of size 52 alloc'd  
==2832==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2832==    by 0x8049062: upload_build_buffer (uftp_client.c:174)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)
==2832== 
==2832== Invalid write of size 1
==2832==    at 0x4026FCB: strncpy (mc_replace_strmem.c:339)
==2832==    by 0x80490F7: upload_build_buffer (uftp_client.c:189)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)
==2832==  Address 0x419b0c5 is 1 bytes after a block of size 52 alloc'd
==2832==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2832==    by 0x8049062: upload_build_buffer (uftp_client.c:174)
==2832==    by 0x8049606: client_upload_file (uftp_client.c:397)
==2832==    by 0x804A30F: data_connection_proc (uftp_client.c:882)
==2832==    by 0x804AE4B: main (uftp_client.c:1214)

I have no idea how to debug this code..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-11-17 14:44:51

Valgrind 输出告诉您几乎所有内容 - 您在 strncpy 调用中编写的内容超出了缓冲区的末尾。该函数中只有一个 strncpy 调用。因此,从逻辑上讲,您搞乱了缓冲区大小或 strncpy 参数。

The Valgrind output tells you pretty much everything- you wrote past the end of the buffer in a strncpy call. There's only one strncpy call in the function. Thus, logically, you messed up your buffer sizes or strncpy arguments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文