truncating string copy

The majority of our calls to strncpy are in the form
  (void) strncpy(dst, src, n);
  dst[n] = '\0';
so add a new routine, copynchars, which does that.  A few calls care
about strncpy's return value and at least one relies on it only copying a
substring without also terminating the output, but most don't care about
either and none seem to care that `n' ought to have type size_t instead of
int.  The new routine matches our usage better, but I haven't gone through
to change the existing strncpy calls.
This commit is contained in:
nethack.rankin
2007-03-06 03:00:05 +00:00
parent 2adc83e145
commit 5a874440a0
3 changed files with 21 additions and 4 deletions

View File

@@ -476,9 +476,8 @@ deliver_splev_message()
/* lev_message can span multiple lines using embedded newline chars;
any segments too long to fit within in_line[] will be truncated */
for (str = lev_message; *str; str = nl + 1) {
(void)strncpy(in_line, str, sizeof in_line - 1);
in_line[sizeof in_line - 1] = '\0';
if ((nl = index(in_line, '\n')) != 0) *nl = '\0';
/* copying will stop at newline if one is present */
copynchars(in_line, str, (int)(sizeof in_line) - 1);
/* convert_line() expects encrypted input;
it reads from in_line[] and writes to out_line[] */