Skip to content

Commit bd80d15

Browse files
committed
ustar_zeropad(): Wrap strncpy() to silence GCC -Wstringop-truncation
1 parent de5099d commit bd80d15

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

sql/sql_backup.cc

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,24 @@ static int backup_stream_write(IF_WIN(HANDLE, int) stream,
656656
return 0;
657657
}
658658

659+
/**
660+
Copy a prefix of a NUL terminated string to a buffer, NUL-padded.
661+
@param b output buffer
662+
@param s NUL terminated string
663+
@param size size of buf, in bytes
664+
*/
665+
static inline char *ustar_zeropad(char *b, const char *s, size_t size) noexcept
666+
{
667+
#if defined __GNUC__ && __GNUC__ >= 8
668+
# pragma GCC diagnostic push
669+
# pragma GCC diagnostic ignored "-Wstringop-truncation"
670+
#endif
671+
return strncpy(b, s, size);
672+
#if defined __GNUC__ && __GNUC__ >= 8
673+
# pragma GCC diagnostic pop
674+
#endif
675+
}
676+
659677
/** Start streaming a file.
660678
@param stream backup stream
661679
@param name file name
@@ -682,14 +700,14 @@ int backup_stream_start(IF_WIN(HANDLE, int) stream,
682700
ustar_block_checksum(buf);
683701
if (int err= backup_stream_write(stream, buf, sizeof buf))
684702
return err;
685-
const size_t whole{s & ~511U};
703+
const size_t whole{s & ~(sizeof buf)};
686704
if (whole)
687705
if (int err= backup_stream_write(stream, name, whole))
688706
return err;
689707
if (s - whole)
690708
{
691-
strncpy(buf, name + whole, 512);
692-
if (int err= backup_stream_write(stream, buf, 512))
709+
ustar_zeropad(buf, name + whole, sizeof buf);
710+
if (int err= backup_stream_write(stream, buf, sizeof buf))
693711
return err;
694712
}
695713
}
@@ -726,15 +744,14 @@ extern "C" int backup_stream_config(IF_WIN(HANDLE, int) stream,
726744
if (int ret=
727745
backup_stream_start(stream, "backup.cnf", 0644, size, nullptr, 0))
728746
return ret;
729-
const size_t whole{size & ~511U};
747+
char buf[512];
748+
const size_t whole{size & ~(sizeof buf)};
730749
if (whole)
731750
if (int err= backup_stream_write(stream, config, whole))
732751
return err;
733752
if (size == whole)
734753
return 0;
735-
736-
char buf[512];
737-
strncpy(buf, config + whole, sizeof buf);
754+
ustar_zeropad(buf, config + whole, sizeof buf);
738755
return backup_stream_write(stream, buf, sizeof buf);
739756
}
740757

0 commit comments

Comments
 (0)