From: Ken Hornstein Date: Sun, 23 Aug 2015 02:16:22 +0000 (-0400) Subject: If str == buffer, then do NOT do a strncpy(buffer, str). Many systems X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/ba8b265a8211b2052b424dc9bd7ea391a8ded87a?ds=sidebyside;hp=acd522420b147129e39215dce421a8b4601d6194 If str == buffer, then do NOT do a strncpy(buffer, str). Many systems this works, but on some systems it causes a SIGABRT. POSIX says the behavior on overlapping copies with str*cpy() is officially undefined. --- diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index ec60e1ba..b0beeb23 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -696,7 +696,8 @@ fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat, if (str) { char *xp; - strncpy(buffer, str, sizeof(buffer)); + if (str != buffer) + strncpy(buffer, str, sizeof(buffer)); buffer[sizeof(buffer)-1] = '\0'; str = buffer; while (isspace((unsigned char) *str)) @@ -889,7 +890,8 @@ fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat, goto unfriendly; if ((str = mn->m_pers) == NULL) { if ((str = mn->m_note)) { - strncpy (buffer, str, sizeof(buffer)); + if (str != buffer) + strncpy (buffer, str, sizeof(buffer)); buffer[sizeof(buffer)-1] = '\0'; str = buffer; if (*str == '(') @@ -933,7 +935,8 @@ fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat, /* UNQUOTEs RFC-2822 quoted-string and quoted-pair */ case FT_LS_UNQUOTE: if (str) { - strncpy(buffer, str, sizeof(buffer)); + if (str != buffer) + strncpy(buffer, str, sizeof(buffer)); /* strncpy doesn't NUL-terminate if it fills the buffer */ buffer[sizeof(buffer)-1] = '\0'; unquote_string(buffer, buffer2);