printf: Prevent special characters not being processed if it was proceeded by a percentage sign (ie '%\n')

This commit is contained in:
2025-07-20 12:35:13 +02:00
parent b0e9193b0a
commit 6bc6000454
2 changed files with 29 additions and 12 deletions

View File

@@ -295,6 +295,8 @@ printf:
;--- Invalid ---;
.invalidReplacement:
cmp byte [rdi + 1], '\'
je .invalidReplacement_specialChar; '%\n' would become "'%','\','n'" instead of "'%',EOS" when inserting full invalid specifier.
mov r9w, word [rdi]
mov [printfBuff+r10], r9w
add rdi, 2
@@ -302,6 +304,14 @@ printf:
add r11, 2
inc r14
jmp .process
.invalidReplacement_specialChar:
mov r9b, byte [rdi]
mov [printfBuff+r10], r9b
inc rdi
inc r10
inc r11
inc r14
jmp .process
;--- '%%' ---;
.rep_pct:

View File

@@ -64,14 +64,14 @@ section .rodata
; printf()
msgPrintf db NL,"TEST printf()",NL,EOS
printf1 db TAB,"printf(",DQUO,"He%ll%o there%%%%%!%!%\n",DQUO,"): ",NL,TAB,TAB,EOS
printf1Str db "He%ll%o there%%%%%!%!%",NL,EOS
printf1 db TAB,"printf(",DQUO,"He%ll%0 there%%%%%!%!%\n",DQUO,"): ",NL,TAB,TAB,EOS
printf1Str db "He%ll%0 there%%%%%!%!%\n",EOS
printf2 db TAB,"printf(",DQUO,"Are %s doing %s?\n",DQUO,", printf2Str1, printf2Str2): ",NL,TAB,TAB,EOS
printf2Str db "Are %s doing %s?",NL,EOS
printf2Str db "Are %s doing %s?\n",EOS
printf2Str1 db "you",EOS
printf2Str2 db "okay",EOS
printf3 db TAB,"printf(",DQUO,"%c%c%c%c%c there %c%c %s%c\n",DQUO,", 'H', 'e', 'l', 'l', 'o', 'm', 'y', ",DQUO,"friend",DQUO,", '!'): ",NL,TAB,TAB,EOS
printf3Str db "%c%c%c%c%c there %c%c %s%c",NL,EOS
printf3Str db "%c%c%c%c%c there %c%c %s%c\n",EOS
printf3C1 equ 'H'
printf3C2 equ 'e'
printf3C3 equ 'l'
@@ -82,21 +82,21 @@ section .rodata
printf3Str1 db "friend",EOS
printf3C8 equ '!'
printf4 db TAB,"printf(",DQUO,"%i|%d|%u , %i|%d|%u\n",DQUO,", -50, -50, -50, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF): ",NL,TAB,TAB,EOS
printf4Str db "%i|%d|%u , %i|%d|%u",NL,EOS
printf4Str db "%i|%d|%u , %i|%d|%u\n",EOS
printf5 db TAB,"printf(",DQUO,"%x|%X , %x|%X\n",DQUO,", 0xabcdeffedcba, 0x069bc0e, 666, -1): ",NL,TAB,TAB,EOS
printf5Str db "%x|%X , %x|%X",NL,EOS
printf5Str db "%x|%X , %x|%X\n",EOS
printf6 db TAB,"printf(",DQUO,"%b | %8b | %08b\n",DQUO,", 5, 7, 9): ",NL,TAB,TAB,EOS
printf6Str db "%b | %8b | %08b",NL,EOS
printf6Str db "%b | %8b | %08b\n",EOS
printf7 db TAB,"printf(",DQUO,"%d | %8d | %08d\n",DQUO,", -234, -666, 234): ",NL,TAB,TAB,EOS
printf7Str db "%d | %8d | %08d",NL,EOS
printf7Str db "%d | %8d | %08d\n",EOS
printf8 db TAB,"printf(",DQUO,"%i | %8i | %08i\n",DQUO,", -234, -666, 234): ",NL,TAB,TAB,EOS
printf8Str db "%i | %8i | %08i",NL,EOS
printf8Str db "%i | %8i | %08i\n",EOS
printf9 db TAB,"printf(",DQUO,"%u | %8u | %08u\n",DQUO,", -234, -666, 234): ",NL,TAB,TAB,EOS
printf9Str db "%u | %8u | %08u",NL,EOS
printf9Str db "%u | %8u | %08u\n",EOS
printf10 db TAB,"printf(",DQUO,"%x | %8x | %08x\n",DQUO,", 0xAB0F, 0xBA0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf10Str db "%x | %8x | %08x",NL,EOS
printf10Str db "%x | %8x | %08x\n",EOS
printf11 db TAB,"printf(",DQUO,"%X | %8X | %08X\n",DQUO,", 0xAB0F, 0xBA0F, 0xAB0F): ",NL,TAB,TAB,EOS
printf11Str db "%X | %8X | %08X",NL,EOS
printf11Str db "%X | %8X | %08X\n",EOS
printf12 db TAB,"rax=0x1234567890ABCDEF",NL,TAB,"printf(",DQUO,"\nRAX\t%064b\nEAX\t\t\t\t\t%032b\n AX\t\t\t\t\t\t\t%016b\n AH\t\t\t\t\t\t\t%08b\t\n AL\t\t\t\t\t\t\t\t%08b\n",DQUO,", rax, eax, ax, ah, al): ",NL,TAB,TAB,EOS
printf12Str db "\nRAX\t%064b\nEAX\t\t\t\t\t%032b\n AX\t\t\t\t\t\t\t%016b\n AH\t\t\t\t\t\t\t%08b\t\n AL\t\t\t\t\t\t\t\t%08b\n",EOS
printf13 db TAB,"printf(",DQUO,"%o | %8o | %08o\n",DQUO,", 1500, 1500, 1500): ",NL,TAB,TAB,EOS
@@ -107,6 +107,8 @@ section .rodata
printf15Str db "%p | %#p | %8p | %#8p | %08p | %#08p\n",EOS
printf16 db TAB,"printf(",DQUO,"%x | %#10x | %10x | %#10x | %010x | %#010x\n",DQUO,", str5, str5, str5, str5, strBuff1, strBuff1): ",NL,TAB,TAB,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
printf17Str db "Ran\dom in\v\a\li%\d test%\n",EOS
; strlen()
msgStrlen db NL,"TEST strlen()",NL,EOS
strlen1 db TAB,"strlen(str1): %d",NL,EOS
@@ -444,6 +446,11 @@ _start:
push strBuff1
call printf
add rsp, SIZE_QWORD * 2
lea rdi, [rel printf17]
call print
lea rdi, [rel printf17Str]
call printf
%endif
;---