From afb3b347d50128b80c568743292583329ac6fca3 Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Fri, 5 Nov 2021 00:40:06 +0000 Subject: Suppress a 'helpful' GCC warning for some correct code --- src/microtar.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/microtar.c b/src/microtar.c index e02cf5d..1f22c19 100644 --- a/src/microtar.c +++ b/src/microtar.c @@ -213,9 +213,23 @@ static int header_to_raw(char* raw, const mtar_header_t* h) return rc; raw[TYPE_OFF] = h->type ? h->type : MTAR_TREG; + +#ifdef __GNUC__ +/* Sigh. GCC wrongly assumes the output of strncpy() is supposed to be + * a null-terminated string -- which it is not, and we are relying on + * that fact here -- and tries to warn about 'string truncation' because + * the null terminator might not be copied. Just suppress the warning. */ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstringop-truncation" +#endif + strncpy(&raw[NAME_OFF], h->name, NAME_LEN); strncpy(&raw[LINKNAME_OFF], h->linkname, LINKNAME_LEN); +#ifdef __GNUC__ +# pragma GCC diagnostic pop +#endif + /* Calculate and write checksum */ chksum = checksum(raw); if((rc = print_octal(&raw[CHKSUM_OFF], CHKSUM_LEN-1, chksum))) -- cgit v1.2.3