讨论/题目交流/🏆 第 184 场力扣周赛/
🏆 第 184 场力扣周赛

欢迎小伙伴们在这里交流分享你的参赛心得以及体验。【前往竞赛

image.png

3 分 - 数组中的字符串匹配
4 分 - 查询带键的排列
5 分 - HTML 实体解析器
7 分 - 给 N x 3 网格图涂色的方案数

展开讨论

请教下第一题一直heap buffer flow是什么原因?
char ** stringMatching(char ** words, int wordsSize, int* returnSize){
returnSize = (int *)malloc(sizeof(int));
int i, j, k;
for (i = 0; i < wordsSize; i++) {
printf("%d, %s, %p", wordsSize, words[i], &(words[i]));
}
*returnSize = wordsSize;
return words;
char **matchWords = (char **)malloc(sizeof(char *) * wordsSize);
memset(matchWords, 0, sizeof(char *) * wordsSize);

char *p;
bool isMatch = false;
for (i = 0; i < wordsSize; i++) {
    matchWords[i] = (char *)malloc(sizeof(char) * 100);
    memset(matchWords[i], 0, sizeof(char) * 100);
}
k = 0;
for (i = 0; i < wordsSize; i++) {
    for (j = 0; j < wordsSize; j++) {
        if (i == j) {
            continue;
        }
        p = strstr(words[j], words[i]);
        if (p != NULL) {
            isMatch = true;
            break;
        }
    }
    if (isMatch == true) {
        printf("%d with %d is %s.", k, i, words[i]);
        strncpy(matchWords[k], words[i], strlen(words[i]));
        printf("%d with %d is %s.", k, i, matchWords[k]);
        matchWords[k][strlen(words[i]) + 1] = '\0';
        k++;
        isMatch = false;
    }
}
*returnSize = k;
printf("%d.", k);
for (i = 0; i < k; i++) {
    printf("match words is %p", &(matchWords[i]));
}
return words;

}

我的输入
["mass","as","hero","superhero"]
我的标准输出
4, mass, 0x6030000000104, as, 0x6030000000184, hero, 0x6030000000204, superhero, 0x603000000028

=================================================================

==46==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000030 at pc 0x000000405074 bp 0x7ffe12fc3280 sp 0x7ffe12fc3270
R
READ of size 8 at 0x603000000030 thread T0

#2 0x7f68203db82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

0
0x603000000030 is located 0 bytes to the right of 32-byte region [0x603000000010,0x603000000030)
a
allocated by thread T0 here:

#0 0x7f68213f6f88 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10bf88)

#3 0x7f68203db82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

S
Shadow bytes around the buggy address:

0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

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 00[fa]fa fd fd fd fa fa fa fd fd

0x0c067fff8010: fd fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa

0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 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
S
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

==46==ABORTING

展开全部 46 讨论