]>
granicus.if.org Git - neomutt/commit
fix: strfcpy() improvement
A quick string copying history.
In the beginning was: strcpy (DST, SRC);
If the SRC was bigger than DST, then bad things happened.
Then came: strncpy (DST, SRC, LEN);
If SRC is longer than LEN, then the string in DST isn't NULL terminated.
Bad things happened.
Next, Mutt created a macro strfcpy() based on the BSD function. It
guarantees a length limit AND a NULL termination.
#define strfcpy(DST,SRC,LEN) strncpy(DST,SRC,LEN), *(DST+(LEN)-1)=0
Because of the way it works, it triggers a warning in Coverity (a static
analysis tool). It fills DST (without NULL), then writes the NULL.
My testing shows it works correctly, but I may missed something.
#define strfcpy(DST,SRC,LEN) do { if ((LEN) > 0) { *(DST+(LEN)-1)=0; strncpy(DST,SRC,(LEN)-1); } } while (0)