igzip: Fix and clarify a few code issues in the cli tool

Fixes a few scan build hits. A few are false positives such as a missed free but
better to clarify the code in this case. Others such as calling no-null
functions are made explicit.

Change-Id: Icb001a2bf7024dbaa4b4c87089eda818de830c78
Signed-off-by: Greg Tucker <greg.b.tucker@intel.com>
This commit is contained in:
Greg Tucker 2019-09-04 14:39:01 -07:00
parent 5f45f3f310
commit 1ba280fa09

View File

@ -583,8 +583,9 @@ int compress_file(void)
struct isal_gzip_header gz_hdr; struct isal_gzip_header gz_hdr;
int ret, success = 0; int ret, success = 0;
char *infile_name = global_options.infile_name, *outfile_name = char *infile_name = global_options.infile_name;
global_options.outfile_name; char *outfile_name = global_options.outfile_name;
char *allocated_name = NULL;
char *suffix = global_options.suffix; char *suffix = global_options.suffix;
size_t infile_name_len = global_options.infile_name_len; size_t infile_name_len = global_options.infile_name_len;
size_t outfile_name_len = global_options.outfile_name_len; size_t outfile_name_len = global_options.outfile_name_len;
@ -598,6 +599,7 @@ int compress_file(void)
} }
if (infile_name_len == stdin_file_name_len && if (infile_name_len == stdin_file_name_len &&
infile_name != NULL &&
memcmp(infile_name, stdin_file_name, infile_name_len) == 0) { memcmp(infile_name, stdin_file_name, infile_name_len) == 0) {
infile_name = NULL; infile_name = NULL;
infile_name_len = 0; infile_name_len = 0;
@ -605,9 +607,10 @@ int compress_file(void)
if (outfile_name == NULL && infile_name != NULL && !global_options.use_stdout) { if (outfile_name == NULL && infile_name != NULL && !global_options.use_stdout) {
outfile_name_len = infile_name_len + suffix_len; outfile_name_len = infile_name_len + suffix_len;
outfile_name = malloc_safe(outfile_name_len + 1); allocated_name = malloc_safe(outfile_name_len + 1);
strcpy(outfile_name, infile_name); outfile_name = allocated_name;
strcat(outfile_name, suffix); strncpy(outfile_name, infile_name, infile_name_len + 1);
strncat(outfile_name, suffix, outfile_name_len + 1);
} }
open_in_file(&in, infile_name); open_in_file(&in, infile_name);
@ -615,6 +618,7 @@ int compress_file(void)
goto compress_file_cleanup; goto compress_file_cleanup;
if (infile_name_len != 0 && infile_name_len == outfile_name_len if (infile_name_len != 0 && infile_name_len == outfile_name_len
&& infile_name != NULL && outfile_name != NULL
&& strncmp(infile_name, outfile_name, infile_name_len) == 0) { && strncmp(infile_name, outfile_name, infile_name_len) == 0) {
log_print(ERROR, "igzip: Error input and output file names must differ\n"); log_print(ERROR, "igzip: Error input and output file names must differ\n");
goto compress_file_cleanup; goto compress_file_cleanup;
@ -668,7 +672,6 @@ int compress_file(void)
size_t outbuf_used = 0; size_t outbuf_used = 0;
uint8_t *iptr = inbuf; uint8_t *iptr = inbuf;
uint8_t *optr = outbuf; uint8_t *optr = outbuf;
ret = 0;
for (q = 0; q < MAX_JOBQUEUE - 1; q++) { for (q = 0; q < MAX_JOBQUEUE - 1; q++) {
inbuf_used += BLOCK_SIZE; inbuf_used += BLOCK_SIZE;
@ -741,8 +744,8 @@ int compress_file(void)
} while (!end_of_stream); } while (!end_of_stream);
// Write gzip trailer // Write gzip trailer
ret = fwrite_safe(&crc, sizeof(uint32_t), 1, out, outfile_name); fwrite_safe(&crc, sizeof(uint32_t), 1, out, outfile_name);
ret += fwrite_safe(&total_in, sizeof(uint32_t), 1, out, outfile_name); fwrite_safe(&total_in, sizeof(uint32_t), 1, out, outfile_name);
#else // No compiled threading support but asked for threads > 1 #else // No compiled threading support but asked for threads > 1
assert(1); assert(1);
@ -788,8 +791,8 @@ int compress_file(void)
remove(infile_name); remove(infile_name);
} }
if (global_options.outfile_name == NULL && outfile_name != NULL) if (allocated_name != NULL)
free(outfile_name); free(allocated_name);
return (success == 0); return (success == 0);
} }
@ -804,8 +807,9 @@ int decompress_file(void)
const int terminal = 0, implicit = 1, stripped = 2; const int terminal = 0, implicit = 1, stripped = 2;
int ret = 0, success = 0, outfile_type = terminal; int ret = 0, success = 0, outfile_type = terminal;
char *infile_name = global_options.infile_name, *outfile_name = char *infile_name = global_options.infile_name;
global_options.outfile_name; char *outfile_name = global_options.outfile_name;
char *allocated_name = NULL;
char *suffix = global_options.suffix; char *suffix = global_options.suffix;
size_t infile_name_len = global_options.infile_name_len; size_t infile_name_len = global_options.infile_name_len;
size_t outfile_name_len = global_options.outfile_name_len; size_t outfile_name_len = global_options.outfile_name_len;
@ -814,6 +818,7 @@ int decompress_file(void)
uint32_t file_time; uint32_t file_time;
if (infile_name_len == stdin_file_name_len && if (infile_name_len == stdin_file_name_len &&
infile_name != NULL &&
memcmp(infile_name, stdin_file_name, infile_name_len) == 0) { memcmp(infile_name, stdin_file_name, infile_name_len) == 0) {
infile_name = NULL; infile_name = NULL;
infile_name_len = 0; infile_name_len = 0;
@ -849,10 +854,12 @@ int decompress_file(void)
outfile_name_len = 0; outfile_name_len = 0;
outfile_type = implicit; outfile_type = implicit;
} }
if (outfile_type != terminal) if (outfile_type != terminal) {
outfile_name = malloc_safe(outfile_name_len >= allocated_name = malloc_safe(outfile_name_len >=
MAX_FILEPATH_BUF ? outfile_name_len + MAX_FILEPATH_BUF ? outfile_name_len +
1 : MAX_FILEPATH_BUF); 1 : MAX_FILEPATH_BUF);
outfile_name = allocated_name;
}
} }
open_in_file(&in, infile_name); open_in_file(&in, infile_name);
@ -887,13 +894,16 @@ int decompress_file(void)
if (outfile_type == implicit) if (outfile_type == implicit)
file_time = gz_hdr.time; file_time = gz_hdr.time;
if (outfile_type == stripped || (outfile_type == implicit && outfile_name[0] == 0)) { if (outfile_name != NULL && infile_name != NULL
&& (outfile_type == stripped
|| (outfile_type == implicit && outfile_name[0] == 0))) {
outfile_name_len = infile_name_len - suffix_len; outfile_name_len = infile_name_len - suffix_len;
memcpy(outfile_name, infile_name, outfile_name_len); memcpy(outfile_name, infile_name, outfile_name_len);
outfile_name[outfile_name_len] = 0; outfile_name[outfile_name_len] = 0;
} }
if (infile_name_len != 0 && infile_name_len == outfile_name_len if (infile_name_len != 0 && infile_name_len == outfile_name_len
&& infile_name != NULL && outfile_name != NULL
&& strncmp(infile_name, outfile_name, infile_name_len) == 0) { && strncmp(infile_name, outfile_name, infile_name_len) == 0) {
log_print(ERROR, "igzip: Error input and output file names must differ\n"); log_print(ERROR, "igzip: Error input and output file names must differ\n");
goto decompress_file_cleanup; goto decompress_file_cleanup;
@ -947,8 +957,8 @@ int decompress_file(void)
remove(infile_name); remove(infile_name);
} }
if (global_options.outfile_name == NULL && outfile_name != NULL) if (allocated_name != NULL)
free(outfile_name); free(allocated_name);
return (success == 0); return (success == 0);
} }