From 8378a6219a7aaca2d882afab386e92dd966aa9a0 Mon Sep 17 00:00:00 2001 From: Peter Colberg Date: Thu, 24 Nov 2022 19:36:14 -0500 Subject: [PATCH] pkg_editor: unconditionally null-terminate output of strncpy() Resolves Coverity error "Buffer not null terminated (BUFFER_SIZE)". The previous code was correct but non-idiomatic. Signed-off-by: Peter Colberg --- lib/pkg_editor/src/pkg_editor.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/pkg_editor/src/pkg_editor.c b/lib/pkg_editor/src/pkg_editor.c index 47366098..3bfa921c 100644 --- a/lib/pkg_editor/src/pkg_editor.c +++ b/lib/pkg_editor/src/pkg_editor.c @@ -1389,11 +1389,8 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of, } // Partially initialize the full path name. - strncpy(full_name, dir_name, FULL_NAME_LENGTH); - - if (full_name[FULL_NAME_LENGTH - 1] != '\0') { - full_name[FULL_NAME_LENGTH - 1] = '\0'; - } + strncpy(full_name, dir_name, FULL_NAME_LENGTH - 1); + full_name[FULL_NAME_LENGTH - 1] = '\0'; full_name[name_length - 1] = '/'; @@ -1577,10 +1574,8 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size, ZInfo z_info; int ret; - strncpy(full_name, out_dir, FULL_NAME_LEN); - if (full_name[FULL_NAME_LEN - 1] != '\0') { - full_name[FULL_NAME_LEN - 1] = '\0'; - } + strncpy(full_name, out_dir, FULL_NAME_LEN - 1); + full_name[FULL_NAME_LEN - 1] = '\0'; // Initialize zlib. z_info.strm.zalloc = Z_NULL;