malloc():smallbin双链表损坏

时间:2022-09-06 16:35:48

This code generates an error at runtime in the else:

此代码在运行时在else中生成错误:

non exists *** Error in `./a.out': malloc(): smallbin double linked list corrupted: 0x09faed58 *** Aborted (core dumped)

不存在***`./a.out'错误:malloc():smallbin双链表损坏:0x09faed58 ***中止(核心转储)

bool fileExists(char *file) {
    if (access(file, F_OK) != -1) {
        return true;
    } else {
        return false;
    }
}

void CopyPaste() {
   char *fileName = basename(path);
   char *c = strcat(dest, "/");
   char *newPath = strcat(c, fileName);

   if (fileExists(newPath)) {
       printf("exists\n");
   } else {
       printf("non exists\n");
   }
}

If I change the concatenation code like that:

如果我像这样更改连接代码:

char *newPath = strcat(strcat(dest,"/"),fileName);

it generates this different error:

它会产生这个不同的错误:

Error in `./a.out': corrupted double-linked list

“./a.out”出错:损坏的双链表

What might the problem be?

问题可能是什么?

1 个解决方案

#1


2  

It appears that you are not using strcat correctly. It appears that your code assumes the concatenation occurs into a new buffer internally allocated for you by strcat but that's not the case. strcat actually modifies the buffer pointed to by the first argument and appends the contents of the second buffer to the first.

看来您没有正确使用strcat。您的代码似乎假定连接发生在由strcat内部为您分配的新缓冲区中,但事实并非如此。 strcat实际上修改了第一个参数指向的缓冲区,并将第二个缓冲区的内容追加到第一个参数。

Per the manual:

根据手册:

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

strcat()函数将src字符串附加到dest字符串,覆盖dest末尾的终止空字节('\ 0'),然后添加一个终止空字节。字符串可能不重叠,dest字符串必须有足够的空间用于结果。如果dest不够大,程序行为是不可预测的;缓冲区溢出是攻击安全程序的最佳途径。

In your case:

在你的情况下:

char *dest; dest =  gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));

Will set dest per the return from gtk_file_chooser_get_current_folder which returns a buffer holding the folder name. That buffer does not have extra space in it for you to append to it. If you want to add (append) to the results of that function call, you need to allocate a separate buffer to hold that file name plus whatever you want to append.

将从gtk_file_chooser_get_current_folder返回设置dest,返回一个包含文件夹名称的缓冲区。该缓冲区中没有额外的空间供您附加。如果要向该函数调用的结果添加(追加),则需要分配一个单独的缓冲区来保存该文件名以及您想要追加的内容。

char *new_dest = malloc(SIZE_YOU_NEED);

strcpy(new_dest, dest);   // Copy file name from gtk_file_chooser_get_current_folder
strcat(new_dest, "/");
strcat(new_dest, fileName);

And in this case, you could shortcut the last two lines as:

在这种情况下,您可以将最后两行缩写为:

strcat(strcat(new_dest, "/"), fileName);

Since, according to the manual, strcat returns the first argument pointer back to you as a return value.

因为,根据手册,strcat将第一个参数指针返回给您作为返回值。

#1


2  

It appears that you are not using strcat correctly. It appears that your code assumes the concatenation occurs into a new buffer internally allocated for you by strcat but that's not the case. strcat actually modifies the buffer pointed to by the first argument and appends the contents of the second buffer to the first.

看来您没有正确使用strcat。您的代码似乎假定连接发生在由strcat内部为您分配的新缓冲区中,但事实并非如此。 strcat实际上修改了第一个参数指向的缓冲区,并将第二个缓冲区的内容追加到第一个参数。

Per the manual:

根据手册:

The strcat() function appends the src string to the dest string, overwriting the terminating null byte ('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the dest string must have enough space for the result. If dest is not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

strcat()函数将src字符串附加到dest字符串,覆盖dest末尾的终止空字节('\ 0'),然后添加一个终止空字节。字符串可能不重叠,dest字符串必须有足够的空间用于结果。如果dest不够大,程序行为是不可预测的;缓冲区溢出是攻击安全程序的最佳途径。

In your case:

在你的情况下:

char *dest; dest =  gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));

Will set dest per the return from gtk_file_chooser_get_current_folder which returns a buffer holding the folder name. That buffer does not have extra space in it for you to append to it. If you want to add (append) to the results of that function call, you need to allocate a separate buffer to hold that file name plus whatever you want to append.

将从gtk_file_chooser_get_current_folder返回设置dest,返回一个包含文件夹名称的缓冲区。该缓冲区中没有额外的空间供您附加。如果要向该函数调用的结果添加(追加),则需要分配一个单独的缓冲区来保存该文件名以及您想要追加的内容。

char *new_dest = malloc(SIZE_YOU_NEED);

strcpy(new_dest, dest);   // Copy file name from gtk_file_chooser_get_current_folder
strcat(new_dest, "/");
strcat(new_dest, fileName);

And in this case, you could shortcut the last two lines as:

在这种情况下,您可以将最后两行缩写为:

strcat(strcat(new_dest, "/"), fileName);

Since, according to the manual, strcat returns the first argument pointer back to you as a return value.

因为,根据手册,strcat将第一个参数指针返回给您作为返回值。