Fix non-portable use of dirname(3) in rmdir
Fixes ticket #6: https://todo.sr.ht/~sircmpwn/ctools/6
The implementation of rmdir was dependent on dirname(3) modifying its
source, which isn't portable behavior. This commit ensures that path is
always modified.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Fix pwd to be conformant
Prior to this commit, the pwd implementation was flawed:
- The -L and -P flags were ignored entirely; the program would always
print the physical working directory (returned from getcwd).
- The program relied on non-standard behavior, in which getcwd would
allocate a buffer if the buf argument was NULL. This behavior is
unspecified by POSIX.
- In the event that an error occured, the program would fail without
printing anything to stderr describing which error occured.
This commit fixes all of the above issues. pwd now defaults to printing
the logical working directory (the value of the PWD environment
variable), and falls back to printing the physical working directory
(from getcwd). The -L and -P flags now work as intended, and errors are
handled properly.
The program is still not fully POSIX compliant. From pwd(1p):
-L If the PWD environment variable contains an absolute pathname
of the current directory and **the pathname does not contain
any components that are dot or dot-dot**, pwd shall write this
pathname to standard output ... Otherwise, the -L option shall
behave as the -P option.
As of this commit, no checks are done to verify that PWD doesn't have
any . or .. components. As far as I'm aware, this is the only area in
which the tool is non-conformant.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
tee: Fix missing output file truncation
STATUS: move hash to N/A
This is a shell builtin
tests: make echo tests robust to build directory
env: fix potential NULL pointer dereference
The two points where the elements of environ are accessed need to be
guarded with a NULL check. env can segfault otherwise.
.build.yml: disable ASAN
Not available for Alpine Linux (or musl libc?)
Implement echo
The behaviour of echo command is to print exactly as the arguments are,
unless -n is in which the escape chars are recognized.
Enable -Werror for zero warning build
chown/chgrp: Fix memory leak
$ meson test -C build --verbose chown
ninja: Entering directory `/home/rumpelsepp/Projects/vendor/ctools/build'
ninja: no work to do.
should_handle_simple OK
should_handle_several OK
should_handle_mutex_options OK
should_handle_recursive
=================================================================
==65006==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 65632 byte(s) in 2 object(s) allocated from:
#0 0x7f2a9b3f9ada in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x7f2a9a88160a in __alloc_dir (/usr/lib/libc.so.6+0xc460a)
SUMMARY: AddressSanitizer: 65632 byte(s) leaked in 2 allocation(s).
FAIL
should_handle_recursive_Hflag
=================================================================
==65054==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 65632 byte(s) in 2 object(s) allocated from:
#0 0x7f4cd631fada in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x7f4cd57a760a in __alloc_dir (/usr/lib/libc.so.6+0xc460a)
SUMMARY: AddressSanitizer: 65632 byte(s) leaked in 2 allocation(s).
FAIL
should_handle_recursive_Lflag
=================================================================
==65102==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 131264 byte(s) in 4 object(s) allocated from:
#0 0x7f7dd8029ada in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x7f7dd74b160a in __alloc_dir (/usr/lib/libc.so.6+0xc460a)
SUMMARY: AddressSanitizer: 131264 byte(s) leaked in 4 allocation(s).
FAIL
should_handle_recursive_Pflag
=================================================================
==65150==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 32816 byte(s) in 1 object(s) allocated from:
#0 0x7f7914ad4ada in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:144
#1 0x7f7913f5c60a in __alloc_dir (/usr/lib/libc.so.6+0xc460a)
SUMMARY: AddressSanitizer: 32816 byte(s) leaked in 1 allocation(s).
FAIL
should_handle_owner_change SKIP($ESCALATE undefined)
OK
Passed: 4 tests
Failed: 4 tests
1/1 chown FAIL 0.92 s (exit status 1)
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
head: Unify error path and fix memory leak
$ meson test -C build --verbose head
ninja: Entering directory `/home/rumpelsepp/Projects/vendor/ctools/build'
ninja: no work to do.
should_read_from_stdin
=================================================================
==399092==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 8192 byte(s) in 1 object(s) allocated from:
#0 0x7f28f131ece8 in __interceptor_calloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cc:153
#1 0x55a9faa60582 in head ../src/head.c:32
#2 0x55a9faa60ae9 in main ../src/head.c:82
#3 0x7f28f0708ee2 in __libc_start_main (/usr/lib/libc.so.6+0x26ee2)
SUMMARY: AddressSanitizer: 8192 byte(s) leaked in 1 allocation(s).
FAIL