FIX: No longer allow exceeding printf buffer, instead print an error to stdout. Temporary fix

Later, if buffer is full, print current buffer and reset buffer so that there is no limit.
This commit is contained in:
2025-06-25 16:23:59 +02:00
parent e020fb15c1
commit 01e14d418a

View File

@ -5,8 +5,11 @@ extern itoa
section .rodata section .rodata
NL db 0xA NL db 0xA
bufferLength equ 4096
ERR_buffLen db "<!> ERROR: Failed to complete printf(), reached buffer length!",0xA,0x0
lERR_buffLen equ $-ERR_buffLen-1
section .bss section .bss
printfBuff resb 4096 printfBuff resb bufferLength
printfNBuff resb 32 printfNBuff resb 32
section .text section .text
global print global print
@ -52,18 +55,25 @@ printf:
push rbp push rbp
mov rbp, rsp mov rbp, rsp
push r12 push rbx ; used to check if error must be sent (reached buff len)
push r13 push r12 ; used for writing byted to printBuff
push r13 ; (also) used for writing bytes to printBuff (why again? gotta check that out - probably not needed, just lost count of register usage at some point)
push r14 ; count length of printBuff
xor rbx, rbx
xor r10, r10 xor r10, r10
xor r14, r14
lea r11, [rel printfBuff] lea r11, [rel printfBuff]
.makeStr: .makeStr:
cmp byte [rdi], 0x0 cmp byte [rdi], 0x0
je .finish je .finish
cmp r14, bufferLength-1
je .finish_L
cmp byte [rdi], '%' cmp byte [rdi], '%'
je .replaceArg je .replaceArg
mov r12b, byte [rdi] mov r12b, byte [rdi]
mov byte [r11], r12b mov byte [r11], r12b
inc r14
jmp .continue jmp .continue
.replaceArg: .replaceArg:
cmp byte [rdi+1], 0x0 cmp byte [rdi+1], 0x0
@ -80,6 +90,7 @@ printf:
;--- invalid specifier ---; ;--- invalid specifier ---;
mov byte [r11], '%' mov byte [r11], '%'
inc r14
inc r10 ;assuming args were passed for invalid specifiers ! inc r10 ;assuming args were passed for invalid specifiers !
jmp .continue jmp .continue
@ -172,8 +183,15 @@ printf:
.charToStr: .charToStr:
mov [printfNBuff], dil mov [printfNBuff], dil
inc r14
cmp r14, bufferLength-1
; Print error to stdout
je .finish_L
mov dil, 0 mov dil, 0
mov [printfNBuff+1], dil mov [printfNBuff+1], dil
inc r14
cmp r14, bufferLength-1
je .finish_L
lea rsi, [rel printfNBuff] lea rsi, [rel printfNBuff]
jmp .insertLoop jmp .insertLoop
@ -208,10 +226,13 @@ printf:
;--- Move fetched data to buffer ---; ;--- Move fetched data to buffer ---;
.insertLoop: .insertLoop:
cmp r14, bufferLength-1
je .finish_L
cmp byte [rsi], 0x0 cmp byte [rsi], 0x0
je .s0f je .s0f
mov r13b, byte [rsi] mov r13b, byte [rsi]
mov byte [r11], r13b mov byte [r11], r13b
inc r14
inc rsi inc rsi
inc r11 inc r11
jmp .insertLoop jmp .insertLoop
@ -224,15 +245,32 @@ printf:
inc rdi inc rdi
inc r11 inc r11
jmp .makeStr jmp .makeStr
.finish_L:
mov rbx, 1
.finish: .finish:
mov byte [r11], 0x0 mov byte [r11], 0x0
lea rdi, [rel printfBuff] lea rdi, [rel printfBuff]
call print call print
lea rdi, [rel printfBuff] lea rdi, [rel printfBuff]
call strlen call strlen
test rbx, rbx
jz .final
push r11
mov rax, NR_write
mov rdi, 2
lea rsi, [rel ERR_buffLen]
mov rdx, lERR_buffLen
syscall
.final:
pop r11
pop r14
pop r13 pop r13
pop r12 pop r12
pop rbx
leave leave
ret ret