Skip to content

Commit e3d53f5

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
fixed bug with memrchr(NULL, c, 0)
1 parent cb22351 commit e3d53f5

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

src/libc/memrchr.src

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
public _memrchr
66

77
_memrchr:
8-
ld iy, 0
8+
ld iy, -1
99
add iy, sp
10-
ld hl, (iy + 3)
11-
ld bc, (iy + 9)
12-
dec bc
10+
ld bc, (iy + 10)
11+
sbc hl, hl
1312
add hl, bc
14-
jr c, .ret_zero
15-
inc bc
16-
ld a, (iy + 6)
13+
jr nc, .ret_zero
14+
ld de, (iy + 4)
15+
add hl, de
16+
ld a, (iy + 7)
1717
cpdr
1818
inc hl
1919
ret z ; found match
20+
; or a, a ; carry won't be set unless (ptr + size) wraps-around (which is UB)
2021
.ret_zero:
2122
; return NULL
22-
or a, a
2323
sbc hl, hl
2424
ret

test/standalone/asprintf_fprintf/src/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
#define SINK (char*)0xE40000
2626

27+
/* pass NULL into functions without triggering -Wnonnull */
28+
extern void* NULL_ptr;
29+
2730
// prevents Clang from replacing function calls with builtins
2831
#if 1
2932

@@ -543,6 +546,9 @@ int strncmp_test(void) {
543546
}
544547

545548
int memrchr_test(void) {
549+
C(T_memrchr(NULL_ptr, 0x00, 0) == NULL_ptr);
550+
C(T_memrchr(NULL_ptr, 0xFF, 0) == NULL_ptr);
551+
546552
C(T_memrchr(SINK, 0x00, 0) == NULL);
547553
C(T_memrchr(SINK, 0x00, 1) == SINK);
548554
C(T_memrchr(SINK, 0xFF, 1) == NULL);
@@ -565,6 +571,7 @@ int memrchr_test(void) {
565571
C(T_memrchr(test, 'G', test_strlen) == NULL);
566572
C(T_memrchr(test, 'G', test_size) == NULL);
567573
C(T_memrchr(test0, 'G', sizeof(test0)) == test0);
574+
568575
return 0;
569576
}
570577

test/standalone/asprintf_fprintf/src/rename.asm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ _T_stpcpy := _stpcpy
2020

2121
_T_bzero := _bzero
2222

23+
section .rodata
24+
25+
public _NULL_ptr
26+
_NULL_ptr:
27+
db $00, $00, $00
28+
2329
extern _memset, _memcpy, _memcmp, _memccpy, _mempcpy, _memrchr
2430
extern _strlen, _strcmp, _strncmp, _stpcpy
2531
extern _bzero

0 commit comments

Comments
 (0)