Skip to content

Commit

Permalink
doc: update HACKING wrt strncpy/pstrcpy
Browse files Browse the repository at this point in the history
Reword the section on strncpy: its NUL-filling is important
in some cases.  Mention that pstrcpy's signature is different.

Signed-off-by: Jim Meyering <[email protected]>
Signed-off-by: Anthony Liguori <[email protected]>
  • Loading branch information
Jim Meyering authored and Anthony Liguori committed Oct 5, 2012
1 parent 9310b9b commit 9b9e3ec
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions HACKING
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ emulators.

4. String manipulation

Do not use the strncpy function. According to the man page, it does
*not* guarantee a NULL-terminated buffer, which makes it extremely dangerous
to use. Instead, use functionally equivalent function:
void pstrcpy(char *buf, int buf_size, const char *str)
Do not use the strncpy function. As mentioned in the man page, it does *not*
guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
It also zeros trailing destination bytes out to the specified length. Instead,
use this similar function when possible, but note its different signature:
void pstrcpy(char *dest, int dest_buf_size, const char *src)

Don't use strcat because it can't check for buffer overflows, but:
char *pstrcat(char *buf, int buf_size, const char *s)
Expand Down

0 comments on commit 9b9e3ec

Please sign in to comment.