printf(): Print null pointers (arg for %s = 0) as (null), fixes #4

This commit is contained in:
2025-07-20 22:51:01 +02:00
parent ebee21c457
commit 8c25b7913b
2 changed files with 33 additions and 0 deletions

View File

@@ -359,6 +359,21 @@ printf:
;--- Insert string to buffer ---; ;--- Insert string to buffer ---;
.insertString: .insertString:
test rsi, rsi
jnz .doInsertString
;rsi = NULL:
mov r9b, 3
cmp r10, printfBuffLen-7
je .flushBuffer
.flushReturn_3:
mov byte [printfBuff + r10], '('
mov qword [printfBuff + r10 + 1], 'null'
mov byte [printfBuff + r10 + 5], ')'
add r10, 6
add r11, 6
jmp .endInsertString
.doInsertString:
cmp byte [rsi], EOS cmp byte [rsi], EOS
je .endInsertString je .endInsertString
mov r9b, 1 mov r9b, 1
@@ -409,6 +424,8 @@ printf:
je .flushReturn_1 je .flushReturn_1
cmp r9b, 2 cmp r9b, 2
je .flushReturn_2 je .flushReturn_2
cmp r9b, 3
je .flushReturn_3
.wrapup: .wrapup:
mov rax, NR_write mov rax, NR_write

View File

@@ -109,6 +109,8 @@ section .rodata
printf16Str db "%x | %#10x | %10x | %#10x | %010x | %#010x\n",EOS printf16Str db "%x | %#10x | %10x | %#10x | %010x | %#010x\n",EOS
printf17 db TAB,"printf(",DQUO,"Ran\dom in\v\a\li%\d test%\n",DQUO,"): ",NL,TAB,TAB,EOS printf17 db TAB,"printf(",DQUO,"Ran\dom in\v\a\li%\d test%\n",DQUO,"): ",NL,TAB,TAB,EOS
printf17Str db "Ran\dom in\v\a\li%\d test%\n",EOS printf17Str db "Ran\dom in\v\a\li%\d test%\n",EOS
printf18 db TAB,"printf(",DQUO,"atoi(12345, 9, 0, 0): %s <<should print (null)\n",DQUO,", atoi(12345, 9, 0, 0)):",NL,TAB,TAB,EOS
printf18Str db "atoi(12345, 9, 0, 0): %s <<should print (null)\n",EOS
; strlen() ; strlen()
msgStrlen db NL,"TEST strlen()",NL,EOS msgStrlen db NL,"TEST strlen()",NL,EOS
strlen1 db TAB,"strlen(str1): %d",NL,EOS strlen1 db TAB,"strlen(str1): %d",NL,EOS
@@ -447,10 +449,24 @@ _start:
call printf call printf
add rsp, SIZE_QWORD * 2 add rsp, SIZE_QWORD * 2
; TEST 17
lea rdi, [rel printf17] lea rdi, [rel printf17]
call print call print
lea rdi, [rel printf17Str] lea rdi, [rel printf17Str]
call printf call printf
; TEST 18
lea rdi, [rel printf18]
call print
mov rdi, 12345
mov rsi, 9
xor rdx, rdx
xor rcx, rcx
call itoa
lea rdi, [rel printf18Str]
mov rsi, rax
call printf
%endif %endif
;--- ;---