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?

    • xigoi
      link
      fedilink
      211 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.

    • @cmeerw
      link
      111 months ago

      name[50] is out of bounds for char name[50]'