cross-posted from: https://lemm.ee/post/4890334

cross-posted from: https://lemm.ee/post/4890282

let’s say I have this code

` #include #include char name[50]; int main(){ fgets(name,50,stdin); name[strcspn(name, “\n”)] = ‘\0’; printf(“hi %s”, name); }

` and I decide my name is “ewroiugheqripougheqpiurghperiugheqrpiughqerpuigheqrpiugherpiugheqrpiughqerpioghqe4r”, my program will throw some unexpected behavior. How would I mitigate this?

  • frankfurt_schoolgirl [she/her]@hexbear.net
    link
    fedilink
    English
    arrow-up
    13
    ·
    1 year ago

    If you want to accept a user input of any length, you have to read the input piece by piece and allocate a new buffer if the original becomes full. Basic steps would be:

    1. Use malloc to make a char * buffer
    2. Read one character at a time in a while loop, keep track of your position in the buffer
    3. If you get an EOF character, add a \0 to your buffer and break the loop. You’re done!
    4. If the position is greater than the length, allocate a new buffer that has twice the length. Use memcpy to copy the stuff from the old buffer to the new one. Use free to get rid of the old buffer.

    This will work until you fill the entire memory of your computer. You should probably set a max length and print an error if it is reached.

    • buh [she/her]@hexbear.net
      link
      fedilink
      English
      arrow-up
      7
      ·
      1 year ago

      this is the right answer for the question, the only thing I would suggest is in step 4, to use realloc instead of doing malloc/memcpy/free cycles, since realloc does all that and will simply extend the allocated space so it can skip the memcpy and free steps if possible, which is a little faster

    • xigoi@lemmy.sdf.org
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago
      1. Realize that you’ll have to do something like this every time you want to work with a string of unknown size
      2. Cry
      3. Use a different language
  • mo_ztt ✅@lemmy.world
    link
    fedilink
    English
    arrow-up
    7
    ·
    1 year ago
    1. In almost all cases you want to be using some encapsulated string class (glib string, C++ string, something like that). The reason is that your question honestly doesn’t really have a good answer. I.e., if you’re storing the name in a statically-allocated char buffer, someone has a name that’s more than 50 characters, you’re screwed. “Screwed” can include all kinds of things up to and include crashing your program or introducing a way for people to enter a malicious name and take over your program.
    2. If you’re really set on doing this, e.g. you just want to do this learn about C memory management, then probably what you want is a dynamic buffer. Find out the length of the name before you allocate the place for it, use malloc() to get a buffer of the size you actually need, and put the string there. It’s highly unusual that in a modern application, doing this type of thing yourself is worth the effort and risk it creates though.
  • paysrenttobirds
    link
    fedilink
    arrow-up
    6
    ·
    1 year ago

    Hello, ewroiugheqripougheqpiurghperiugheqrpiughqerpuigheq,

    The fgets function will only read in as many characters as you tell it to (50) in the second parameter, so the rest of the input will simply be lost and the name will be truncated.

    • fubo@lemmy.world
      link
      fedilink
      arrow-up
      7
      ·
      edit-2
      1 year ago

      And, mind you, fgets is the safer replacement to the original gets, which attempts to read a variable-length line into a fixed-length buffer.

      The manual has this to say about gets

      BUGS
             Never use gets().  Because it is impossible to tell without knowing the
             data  in  advance  how  many  characters  gets() will read, and because
             gets() will continue to store characters past the end of the buffer, it
             is  extremely dangerous to use.  It has been used to break computer se‐
             curity.  Use fgets() instead.
      
      • mo_ztt ✅@lemmy.world
        link
        fedilink
        English
        arrow-up
        3
        ·
        1 year ago

        Why is this even still in the library 🥲

        Twenty years ago it kind of made sense. Ok it’s bad, but sometimes we’re just reading a local file fully under our control, maybe from old code that the source doesn’t exist anymore for, it’s such a core function that taking it out however badly needed will have some negative consequences.

        At this point though, I feel like calling it should just play a loud, stern “NO!” over your speakers and exit the program.

        • fubo@lemmy.world
          link
          fedilink
          arrow-up
          6
          ·
          1 year ago

          Why is this even still in the library 🥲

          The linker will complain at you —

          dumb.c:(.text+0x2f): warning: the `gets' function is dangerous and should not be used.
          
  • nothacking@discuss.tchncs.de
    link
    fedilink
    arrow-up
    4
    ·
    1 year ago

    Your going to have to read the input one piece at a time, allocating a bigger buffer as you go. (realloc is the way to go here) I recommend putting the input reading code in a function do you can easily use it multiple times.