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]
    ·
    11 months 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 [any]
      ·
      11 months 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
      ·
      11 months 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
  • DirigibleProtein@aussie.zone
    ·
    11 months ago

    Can’t help with your code, but you should be aware of:

    • https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/
    • https://jalopnik.com/this-hawaiian-womans-name-is-too-long-for-a-drivers-l-1313683178
    • hairyballs@programming.dev
      ·
      edit-2
      11 months ago

      The first article is funny, because I moved from my native country to the one right next to it, and everybody is confused by my name. They have one given name and 2 family names, while I have 4 first names, and a compound last name.

      No need to travel to the other side of the planet to meet a different culture of naming.

  • nothacking@discuss.tchncs.de
    ·
    11 months 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.

  • buh [any]
    ·
    edit-2
    11 months ago

    I dont really see the point of the 2nd line in main, fgets already makes sure to null terminate

    • xigoi@lemmy.sdf.org
      ·
      11 months ago

      Aside from the obvious effect of truncating long names, this could be dangerous if the 50-byte boundary is in the middle of a Unicode codepoint.

  • PaX [comrade/them, they/them]
    ·
    edit-2
    11 months ago

    In addition to the other answers, you might consider using getline() if you're on a POSIX-compliant system. It can automatically allocate a buffer for you.