开发者

nasm, read syscall reads over buffer size

开发者 https://www.devze.com 2023-04-13 06:30 出处:网络
I have the following code. It works ok except one thing which limits its usage in other programs. When I run it in the debugger, Linux read system call returns value always bigger than the specified b

I have the following code. It works ok except one thing which limits its usage in other programs. When I run it in the debugger, Linux read system call returns value always bigger than the specified buffer size. Why is it and how to fix it, because it doesn't let the program to loop through the buffer array without a segmentation fault?

    SECTION .data
address dd "log.txt", 0
badf dd "Bad file!",0
buffsize dd 1024
size dd 1024
filedesc dd 0

section .bss  开发者_JS百科  
buf     resb    1024


    SECTION .text       
        global main     
main:   
            mov   ebx, address
            mov   eax,  5           ; open(
            mov   ecx,  0           ;   read-only mode
            int   80h               ; );
            mov   [filedesc], eax
read_loop:

            mov     ebx,  [filedesc]       ;   file_descriptor, 
            mov     eax,  3         ; read(     
            mov     ecx,  buf       ;   *buf,
            mov     edx,  buffsize     ;   *bufsize
            int     80h             ; );
            test    eax,  eax
            jz      done
        js badfile              

            mov     eax,  4         ; write(
            mov     ebx,  1         ;   STDOUT,
            mov     edx,  buffsize
            mov     ecx,  buf       ;   *buf
            int     80h     

            jmp     read_loop
badfile:
            mov     eax,  4         ; write(
            mov     ebx,  1         ;   STDOUT,
            mov     edx,  10
            mov     ecx,  badf       ;   *buf
            int     80h     

done:
    mov eax, 6
    mov ebx, [filedesc]
    int 0x80


    mov ebx,0       
    mov eax,1       
    int 0x80   


        mov     edx,  buffsize     ;   *bufsize

Is wrong since buffsize is declared as follows:

        buffsize dd 1024

the above code will move the address of buffsize to edx. What you want is:

        mov edx, [buffsize]

which will move the value stored at buffsize to edx.

You have a few of those type of errors in there.


Could it be a negative error return code?

I don't see any test in your code for negative values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号