Author Topic: SYS_READ and overflows. How to avoid them?  (Read 15927 times)

nobody

  • Guest
SYS_READ and overflows. How to avoid them?
« on: August 08, 2008, 01:34:58 PM »
Hi all,

how can I prevent overflows when using the "read" syscall? How is it done?

Regards,
chris

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: SYS_READ and overflows. How to avoid them?
« Reply #1 on: August 08, 2008, 03:49:24 PM »
A "read" system call should have a parameter for "bytes to read" - in edx for Linux, on the stack for Windows, BSD, Mac(?)... Make sure your buffer is big enough to hold it. If you propose to zero-terminate the input, you may want to add one to the buffer for the zero (unless you're overwriting a linefeed).

If you're reading from stdin, the call won't return until a linefeed is encountered. If the pesky user just keeps typing, you'll get "bytes to read" bytes in your buffer, and any excess - including the linefeed - will be in an OS buffer... which will show up in the next "read" - possibly on the command line after your program exits (yuck!). You can flush this OS buffer by reading one byte at a time until you find the linefeed, if it isn't in "your" buffer...

If that doesn't answer the question, try giving us a more specific example of what you're trying to do...

Best,
Frank