Thread: Best way to prevent buffer-overflow

  1. #1
    Registered User
    Join Date
    Jan 2007

    Best way to prevent buffer-overflow

    I'm making a network program/game and I want to prevent clients from up the buffers.

    char buf[x] has a limit... is there a way to make x dependent on user input? (strlen or something).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Read everything into a fixed-length buffer initially. All the good input functions allow you to specify a buffer and a length.

    When you've got the buffer and its actual length, then allocate whatever memory you need to store that (linked list, array, whatever) and copy from your input buffer to you allocated memory.

    Rinse and repeat until you have all the input you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. buffer overflow
    By cpp_is_fun in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 11:04 PM
  3. Buffer overflow errors
    By EvBladeRunnervE in forum C Programming
    Replies: 2
    Last Post: 03-17-2004, 04:58 PM
  4. Buffer overflow issue.
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 12-27-2003, 12:13 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM