AddressSanitiser: SEGV on unknown address

你。 提交于 2019-12-11 16:52:40

问题


I am getting the error

SEGV on unknown address

while running my program. I am pretty sure it is coming from fgets() but i am not quite sure why. Before this, I was using scanf() and it was working fine. the input was going into the array just fine, but I had to use fgets() for the program to detect an input of a new line. Can anyone help me understand why I am getting this error and suggest a way to fix this problem?

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    int x = -1;
    int y = -1;

    scanf("%d %d", &x, &y);

    if (x <= 0 || y <= 0) {
        printf("Cannot decode\n");
        return 1;
    }

    char *temp = 0;
    int v[x][y];

    for (int t=0; t < y; t++) {
        for(int i=0; i < x; i++) {

            if (fgets(temp, x, stdin)) {
                v[i][t] = atoi(temp);
            }
        }
    }

    // To test array
    int c, o;
    for(o=0; o < x; o++) {
        for(c=0; c < y; c++) {
            printf("%d", v[c][o]);

        }
        printf("\n");
    }

    return 0;
}

Error code:

ASAN:DEADLYSIGNAL
=================================================================
==19==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x7fcaaf49d4ab bp 0x
000000000001 sp 0x7ffc84b842b8 T0)
==19==The signal is caused by a WRITE memory access.
==19==Hint: address points to the zero page.
#0 0x7fcaaf49d4aa in __memmove_avx_unaligned_erms (/usr/lib/libc.so.6+0x1574aa)
#1 0x7fcaaf3b4708 in __GI__IO_getline_info (/usr/lib/libc.so.6+0x6e708)
#2 0x7fcaaf3b353c in fgets (/usr/lib/libc.so.6+0x6d53c)
#3 0x5001f4 in main /home/nonogram.c:39:11
#4 0x7fcaaf366f49 in __libc_start_main (/usr/lib/libc.so.6+0x20f49)
#5 0x4187d9 in _start (/home/nonogram+0x4187d9)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/usr/lib/libc.so.6+0x1574aa) in __memmove_avx_unaligned_erms
==19==ABORTING

回答1:


You have to allocate memory to store the information you are reading. Reserve memory with calloc or malloc for temp pointer.

char *temp = calloc(length_you_expect, sizeof(char))

instead of:

char *temp = 0

And remember to free after you use it:

free(temp)



来源:https://stackoverflow.com/questions/49794189/addresssanitiser-segv-on-unknown-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!