~sircmpwn/ctools

291b64b0b0aa23a3a180f4cf03069c5dff91129b — Stefan Tatschner 1 year, 21 days ago c77ee19
cat: Fix stack use after scope

$ meson test -C build --verbose cat
ninja: Entering directory `/home/rumpelsepp/Projects/vendor/ctools/build'
[4/4] Linking target head.
should_handle_ddash     OK
should_handle_one_file  OK
should_handle_two_files OK
should_handle_stdin     =================================================================
==400903==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7ffeef650a60 at pc 0x55ea6a4fc991 bp 0x7ffeef650a10 sp 0x7ffeef650a00
READ of size 8 at 0x7ffeef650a60 thread T0
    #0 0x55ea6a4fc990 in main ../src/cat.c:77
    #1 0x7fb4f253bee2 in __libc_start_main (/usr/lib/libc.so.6+0x26ee2)
    #2 0x55ea6a4fc1dd in _start (/home/rumpelsepp/Projects/vendor/ctools/build/cat+0x21dd)

Address 0x7ffeef650a60 is located in stack of thread T0 at offset 32 in frame
    #0 0x55ea6a4fc6de in main ../src/cat.c:53

  This frame has 1 object(s):
    [32, 40) '<unknown>' <== Memory access at offset 32 is inside this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-use-after-scope ../src/cat.c:77 in main
Shadow bytes around the buggy address:
  0x10005dec20f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x10005dec2140: 00 00 00 00 00 00 00 00 f1 f1 f1 f1[f8]f3 f3 f3
  0x10005dec2150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x10005dec2190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
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
==400903==ABORTING
FAIL
should_handle_u_flag    FAIL
Passed: 3 tests
Failed: 2 tests
1/1 cat                                     FAIL     0.12 s (exit status 1)

Ok:                    0
Expected Fail:         0
Fail:                  1
Unexpected Pass:       0
Skipped:               0
Timeout:               0
1 files changed, 2 insertions(+), 1 deletions(-)

M src/cat.c
M src/cat.c => src/cat.c +2 -1
@@ 65,10 65,11 @@ main(int argc, char *argv[])
		}
	}

	char *def[] = { "-" };
	if (optind == argc) {
		optind = 0;
		argc = 1;
		argv = (char *[]){ "-" };
		argv = def;
	}

	for (int i = optind; i < argc; ++i) {