Best way to prevent buffer-overflow

This is a discussion on Best way to prevent buffer-overflow within the C Programming forums, part of the General Programming Boards category; I'm making a network program/game and I want to prevent clients from ........ing up the buffers. char buf[x] has a ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    16

    Best way to prevent buffer-overflow

    I'm making a network program/game and I want to prevent clients from ........ing 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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04: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, 03:58 PM
  4. Buffer overflow issue.
    By caroundw5h in forum C Programming
    Replies: 3
    Last Post: 12-27-2003, 11:13 AM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21