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
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
printfBuff resb 4096
printfBuff resb bufferLength
printfNBuff resb 32
section .text
global print
@ -52,18 +55,25 @@ printf:
push rbp
mov rbp, rsp
push r12
push r13
push rbx ; used to check if error must be sent (reached buff len)
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 r14, r14
lea r11, [rel printfBuff]
.makeStr:
cmp byte [rdi], 0x0
je .finish
cmp r14, bufferLength-1
je .finish_L
cmp byte [rdi], '%'
je .replaceArg
mov r12b, byte [rdi]
mov byte [r11], r12b
inc r14
jmp .continue
.replaceArg:
cmp byte [rdi+1], 0x0
@ -80,6 +90,7 @@ printf:
;--- invalid specifier ---;
mov byte [r11], '%'
inc r14
inc r10 ;assuming args were passed for invalid specifiers !
jmp .continue
@ -172,8 +183,15 @@ printf:
.charToStr:
mov [printfNBuff], dil
inc r14
cmp r14, bufferLength-1
; Print error to stdout
je .finish_L
mov dil, 0
mov [printfNBuff+1], dil
inc r14
cmp r14, bufferLength-1
je .finish_L
lea rsi, [rel printfNBuff]
jmp .insertLoop
@ -208,10 +226,13 @@ printf:
;--- Move fetched data to buffer ---;
.insertLoop:
cmp r14, bufferLength-1
je .finish_L
cmp byte [rsi], 0x0
je .s0f
mov r13b, byte [rsi]
mov byte [r11], r13b
inc r14
inc rsi
inc r11
jmp .insertLoop
@ -224,15 +245,32 @@ printf:
inc rdi
inc r11
jmp .makeStr
.finish_L:
mov rbx, 1
.finish:
mov byte [r11], 0x0
lea rdi, [rel printfBuff]
call print
lea rdi, [rel printfBuff]
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 r12
pop rbx
leave
ret