0

I'm solving the Defanging an IP Address Question on Leetcode, and it works fine when I run it locally:

char * defangIPaddr(char * address){
    char *defangedAddress;
    defangedAddress = malloc(sizeof(address) + 10);
    int j = 0;
    
    int length = strlen(address);
    for (int i = 0; i < length; i++)
    {
        if(address[i] == '.')
        {
            defangedAddress[j] = '[';
            defangedAddress[j+1] = '.';
            defangedAddress[j+2] = ']';
            j = j+3;
        }
        else
        {
            defangedAddress[j] = address[i];
            j++;
        }
    }
    return defangedAddress;
}

But I get this error when I try to run on leetcode:

=================================================================
==33==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000022 at pc 0x000000402033 bp 0x7ffdcaf4f350 sp 0x7ffdcaf4f340
READ of size 1 at 0x603000000022 thread T0
    #3 0x7f741cc6b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
0x603000000022 is located 0 bytes to the right of 18-byte region [0x603000000010,0x603000000022)
allocated by thread T0 here:
    #0 0x7f741dc86f88 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10bf88)
    #3 0x7f741cc6b82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
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[02]fa fa fa fa fa fa fa fa fa fa fa
  0x0c067fff8010: fa 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
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
==33==ABORTING

I tried installing GDB on my local to debug, though, well, I get a GDB error:

(No debugging symbols found in ./defanging-an-ip-address)

And I've seen Valgrind but not had a lot of experience in it, so I'm totally blanking on how to debug and figure this out.

I know I can solve it in another language (I have some experience in JS, PHP, Python), but I want to nail C and hence trying to figure out where I'm going wrong and what I need to do to fix it, without getting discouraged and losing hope.

Thanks!

echorashmi
  • 29
  • 4
  • `sizeof(address)` doesn't do what you think it does. It returns the size of a pointer, not the size of the array that it points to. – Barmar Aug 04 '20 at 20:14
  • You probably want `strlen(address) + 11`. And don't forget to add a null terminator to the string. – Barmar Aug 04 '20 at 20:18

0 Answers0