讨论/题目交流/leetcode 49. 字母异位词分组/
leetcode 49. 字母异位词分组

麻烦各位大佬帮忙看一下为什么会报错

char *** groupAnagrams(char **strs, int strsSize, int *returnSize, int **returnColumnSizes)
{
    *returnSize = 0;
    *returnColumnSizes = (int *)malloc(sizeof(int) * strsSize);
    int index1 = 0;
    int index2 = 0;
    int *sign = (int *)calloc(strsSize, sizeof(int));
    char ***res = (char ***)malloc(sizeof(char **) * strsSize);
    for (int i = 0; i < strsSize; i++) {
        if (sign[i] == 1) {
            continue;
        }
        index2 = 0;
        res[index1] = (char **)malloc(sizeof(char *) * strsSize);
        res[index1][index2] = (char *)malloc(sizeof(char) * 20);
        char *p = strs[i];
        int wordsign[26] = {0};
        while (*p != '\0') {
            wordsign[*p - 'a'] += 1;
            p++;
        }
        strncpy(res[index1][index2], strs[i], strlen(strs[i]));
        index2++;
        sign[i] = 1;
        for (int j = i + 1; j < strsSize; j++) {
            if (sign[j] == 1) {
                continue;
            }
            int m;
            for (m = 0; m < strlen(strs[j]); m++) {
                if (wordsign[strs[j][m] - 'a'] == 0) {
                    break;
                }
                p++;
            }
            if (m == strlen(strs[j])) {
                res[index1][index2] = (char *)malloc(sizeof(char) * 20);
                strncpy(res[index1][index2], strs[j], strlen(strs[j]));
                sign[j] = 1;
                index2++;
            }
        }
        index1++;
        (*returnColumnSizes)[*returnSize] = index2;
        (*returnSize)++;
    }
    return res;
}

错误信息:

==47==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000084 at pc 0x000000403000 bp 0x7ffced55cd60 sp 0x7ffced55cd50
READ of size 1 at 0x603000000084 thread T0
#4 0x7f448b6ca82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x603000000084 is located 0 bytes to the right of 20-byte region [0x603000000070,0x603000000084)
allocated by thread T0 here:
#0 0x7f448c6e6078 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10c078)
#3 0x7f448b6ca82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
Shadow bytes around the buggy address:
0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c067fff8000: fa fa 00 00 00 fa fa fa 00 00 00 fa fa fa 00 00
=>0x0c067fff8010:[04]fa fa fa 00 00 04 fa fa fa 00 00 04 fa fa fa
0x0c067fff8020: 00 00 04 fa fa fa 00 00 04 fa fa fa 00 00 04 fa
0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c067fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==47==ABORTING

展开讨论
落羽发起于 2020-05-20

堆内存溢出