0

How can I get line numbering when running address sanitizer on gcc?

I have a simple program to test address santizer (buffer overflow):

#include<stdio.h>


int main(void)
{
    int A[10] = {0};
    A[10] = 1;

}

when running address sanitizer (gcc):

gcc b_oflow.c -fsanitize=address -g 
==7961==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff90e56898 at pc 0x564401bc42be bp 0x7fff90e56840 sp 0x7fff90e56838
WRITE of size 4 at 0x7fff90e56898 thread T0
    #0 0x564401bc42bd in main (/home/victor/Documents/code/C/lisp/src/a.out+0x12bd)
    #1 0x7fbeb1a7009a in __libc_start_main ../csu/libc-start.c:308
    #2 0x564401bc40b9 in _start (/home/victor/Documents/code/C/lisp/src/a.out+0x10b9)

Address 0x7fff90e56898 is located in stack of thread T0 at offset 72 in frame
    #0 0x564401bc4184 in main (/home/victor/Documents/code/C/lisp/src/a.out+0x1184)

How can I get to output the line numbering? I've tried answers from questions: How do I get line numbers in the debug output with clang's -fsanitize=address? and How do I get line numbers in the debug output with clang's -fsanitize=address? but don't know what to do, I wanted to print the line where the overflow occurred when running the executable.

8
  • 1
    Usually, add the -g option to both the compilation to object file and the linking process. Commented Jul 26, 2021 at 6:19
  • I've tried with -g now and continues to print the address but not line numbering. Commented Jul 26, 2021 at 6:22
  • Which version of GCC are you using? Commented Jul 26, 2021 at 6:31
  • 2
    I can't replicate your problem in the compiler explorer. Even without the -g option I get line-numbers. Only if I add the -g0 option (to explicitly disable all debug information) won't I get it. The -g option should really make sure you get the line-number. Commented Jul 26, 2021 at 6:35
  • 1
    I cannot reproduce. I copied the file and command as-is and it worked. ../csu/libc-start.c:308 - Are you using some LD_PRELOAD? Or specifying custom configuration with custom startup? Your compiler should have linked with libc.so.6. Please post the output of gcc -v ..... Please try compiling and running in a new fresh shell. Please try compiling with env -i PATH=$PATH gcc ... and then running with env -i ./a.out??
    – KamilCuk
    Commented Jul 26, 2021 at 6:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.