igzip: exit with status 1 in applications upon failure

Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
This commit is contained in:
Pablo de Lara
2025-09-23 08:51:11 +00:00
parent 9f77f65dbc
commit bfc99b6a18
9 changed files with 75 additions and 59 deletions

View File

@@ -48,17 +48,17 @@ main(int argc, char *argv[])
if (argc != 3) {
fprintf(stderr, "Usage: igzip_example infile outfile\n");
exit(0);
exit(1);
}
in = fopen(argv[1], "rb");
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
exit(0);
exit(1);
}
out = fopen(argv[2], "wb");
if (!out) {
fprintf(stderr, "Can't open %s for writing\n", argv[2]);
exit(0);
exit(1);
}
printf("igzip_example\nWindow Size: %d K\n", IGZIP_HIST_SIZE / 1024);
@@ -74,7 +74,7 @@ main(int argc, char *argv[])
stream.level_buf_size = ISAL_DEF_LVL1_DEFAULT;
if (stream.level_buf == 0) {
printf("Failed to allocate level compression buffer\n");
exit(0);
exit(1);
}
}

View File

@@ -103,7 +103,7 @@ usage(void)
" -d <file> dictionary file used by compression\n"
" -w <size> log base 2 size of history window, between 8 and 15\n");
exit(0);
exit(1);
}
void
@@ -206,13 +206,13 @@ main(int argc, char *argv[])
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", in_file_name);
exit(0);
exit(1);
}
if (out_file_name != NULL) {
out = fopen(out_file_name, "wb");
if (!out) {
fprintf(stderr, "Can't open %s for writing\n", out_file_name);
exit(0);
exit(1);
}
printf("outfile=%s\n", out_file_name);
}
@@ -221,7 +221,7 @@ main(int argc, char *argv[])
dict = fopen(dict_file_name, "rb");
if (!dict) {
fprintf(stderr, "Can't open %s for reading\n", dict_file_name);
exit(0);
exit(1);
}
printf("outfile=%s\n", dict_file_name);
}
@@ -241,27 +241,39 @@ main(int argc, char *argv[])
* (assuming some possible expansion on output size)
*/
infile_size = get_filesize(in);
if (infile_size == 0) {
/* Check if it's a real error or just an empty file */
if (fseek(in, 0, SEEK_END) != 0 || ftell(in) < 0)
fprintf(stderr, "Failed to get file size\n");
else
fprintf(stderr, "Input file has zero length\n");
exit(1);
}
outbuf_size = 2 * infile_size + BUF_SIZE;
dictfile_size = (dict_file_name != NULL) ? get_filesize(dict) : 0;
if (dict_file_name != NULL && dictfile_size == 0) {
fprintf(stderr, "Failed to get dictionary file size or dictionary is empty\n");
exit(1);
}
inbuf = malloc(infile_size);
if (inbuf == NULL) {
fprintf(stderr, "Can't allocate input buffer memory\n");
exit(0);
exit(1);
}
outbuf = malloc(outbuf_size);
if (outbuf == NULL) {
fprintf(stderr, "Can't allocate output buffer memory\n");
exit(0);
exit(1);
}
if (dictfile_size != 0) {
dictbuf = malloc(dictfile_size);
if (dictbuf == NULL) {
fprintf(stderr, "Can't allocate dictionary buffer memory\n");
exit(0);
exit(1);
}
}
@@ -269,7 +281,7 @@ main(int argc, char *argv[])
level_buf = malloc(level_size);
if (level_buf == NULL) {
fprintf(stderr, "Can't allocate level buffer memory\n");
exit(0);
exit(1);
}
}
@@ -281,13 +293,13 @@ main(int argc, char *argv[])
stream.avail_in = (uint32_t) fread(inbuf, 1, infile_size, in);
if (stream.avail_in != infile_size) {
fprintf(stderr, "Couldn't fit all of input file into buffer\n");
exit(0);
exit(1);
}
/* Read complete dictionary into buffer */
if ((dictfile_size != 0) && (dictfile_size != fread(dictbuf, 1, dictfile_size, dict))) {
fprintf(stderr, "Couldn't fit all of dictionary file into buffer\n");
exit(0);
exit(1);
}
struct isal_dict dict_str;
@@ -307,7 +319,7 @@ main(int argc, char *argv[])
}
if (stream.avail_in != 0) {
fprintf(stderr, "Could not compress all of inbuf\n");
exit(0);
exit(1);
}
printf(" file %s - in_size=%lu out_size=%d ratio=%3.1f%%", in_file_name, infile_size,
@@ -329,7 +341,7 @@ main(int argc, char *argv[])
if (stream.avail_in != 0) {
fprintf(stderr, "Could not compress all of inbuf\n");
exit(0);
exit(1);
}
printf("igzip_file: ");

View File

@@ -82,12 +82,12 @@ main(int argc, char *argv[])
fprintf(stderr, "Usage: igzip_file_perf infile [outfile]\n"
"\t - Runs multiple iterations of igzip on a file to "
"get more accurate time results.\n");
exit(0);
exit(1);
}
in = fopen(argv[1], "rb");
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
exit(0);
exit(1);
}
/* Allocate space for entire input file and output
@@ -106,14 +106,14 @@ main(int argc, char *argv[])
inbuf = malloc(infile_size);
if (inbuf == NULL) {
fprintf(stderr, "Can't allocate input buffer memory\n");
exit(0);
exit(1);
}
avail_in = fread(inbuf, 1, infile_size, in);
if (avail_in != infile_size) {
free(inbuf);
fprintf(stderr, "Couldn't fit all of input file into buffer\n");
exit(0);
exit(1);
}
struct perf start;

View File

@@ -46,20 +46,20 @@ main(int argc, char *argv[])
if (argc != 3) {
fprintf(stderr, "Usage: igzip_inflate_example infile outfile\n");
exit(0);
exit(1);
}
in = fopen(argv[1], "rb");
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
exit(0);
exit(1);
}
out = fopen(argv[2], "wb");
if (!out) {
fprintf(stderr, "Can't open %s for writing\n", argv[2]);
fclose(in);
exit(0);
exit(1);
}
printf("igzip_inflate_example\n");

View File

@@ -57,7 +57,7 @@ inflate_multi_pass(uint8_t *compress_buf, uint64_t compress_len, uint8_t *uncomp
state = malloc(sizeof(struct inflate_state));
if (state == NULL) {
printf("Failed to allocate memory\n");
exit(0);
exit(1);
}
isal_inflate_init(state);
@@ -82,7 +82,7 @@ inflate_multi_pass(uint8_t *compress_buf, uint64_t compress_len, uint8_t *uncomp
if (comp_tmp == NULL) {
printf("Failed to allocate memory\n");
exit(0);
exit(1);
}
memcpy(comp_tmp, compress_buf + comp_processed, comp_tmp_size);
@@ -117,7 +117,7 @@ inflate_multi_pass(uint8_t *compress_buf, uint64_t compress_len, uint8_t *uncomp
uncomp_tmp = malloc(uncomp_tmp_size);
if (uncomp_tmp == NULL) {
printf("Failed to allocate memory\n");
exit(0);
exit(1);
}
state->avail_out = uncomp_tmp_size;
@@ -258,17 +258,17 @@ main(int argc, char **argv)
compressed_stream = malloc(compressed_length);
if (uncompressed_stream == NULL && file_length != 0) {
printf("\nFailed to allocate input memory\n");
exit(0);
exit(1);
}
if (compressed_stream == NULL) {
printf("\nFailed to allocate output memory\n");
exit(0);
exit(1);
}
if (uncompressed_test_stream == NULL && file_length != 0) {
printf("\nFailed to allocate decompressed memory\n");
exit(0);
exit(1);
}
uncompressed_length = (uncompressed_stream == NULL)

View File

@@ -167,7 +167,7 @@ usage(void)
" -b <size> input buffer size, applies to stateful options (-f,-z,-s)\n"
" -y <type> flush type: 0 (default: no flush), 1 (sync flush), 2 (full flush)\n"
" -w <size> log base 2 size of history window, between 9 and 15\n");
exit(0);
exit(1);
}
void
@@ -664,14 +664,14 @@ main(int argc, char *argv[])
case 'l':
if (compression_queue_size >= COMPRESSION_QUEUE_LIMIT) {
printf("Too many levels specified");
exit(0);
exit(1);
}
compress_strat.mode = ISAL_STATELESS;
compress_strat.level = atoi(optarg);
if (compress_strat.level > ISAL_DEF_MAX_LEVEL) {
printf("Unsupported isa-l compression level\n");
exit(0);
exit(1);
}
compression_queue[compression_queue_size] = compress_strat;
@@ -680,14 +680,14 @@ main(int argc, char *argv[])
case 'f':
if (compression_queue_size >= COMPRESSION_QUEUE_LIMIT) {
printf("Too many levels specified");
exit(0);
exit(1);
}
compress_strat.mode = ISAL_STATEFUL;
compress_strat.level = atoi(optarg);
if (compress_strat.level > ISAL_DEF_MAX_LEVEL) {
printf("Unsupported isa-l compression level\n");
exit(0);
exit(1);
}
compression_queue[compression_queue_size] = compress_strat;
@@ -696,14 +696,14 @@ main(int argc, char *argv[])
case 'z':
if (compression_queue_size >= COMPRESSION_QUEUE_LIMIT) {
printf("Too many levels specified");
exit(0);
exit(1);
}
compress_strat.mode = ZLIB;
compress_strat.level = atoi(optarg);
if (compress_strat.level > Z_BEST_COMPRESSION) {
printf("Unsupported zlib compression level\n");
exit(0);
exit(1);
}
compression_queue[compression_queue_size] = compress_strat;
compression_queue_size++;
@@ -722,17 +722,17 @@ main(int argc, char *argv[])
dict_fn = fopen(optarg, "rb");
if (!dict_fn) {
printf("Can't open dictionary for reading\n");
exit(0);
exit(1);
}
dict_file_size = get_filesize(dict_fn);
dict_buf = malloc(dict_file_size);
if (dict_buf == NULL || dict_file_size == 0) {
printf("Can't allocate mem for dictionary buffer\n");
exit(0);
exit(1);
}
if (dict_file_size != fread(dict_buf, 1, dict_file_size, dict_fn)) {
printf("Couldn't read all of dictionary file\n");
exit(0);
exit(1);
}
fclose(dict_fn);
break;
@@ -754,7 +754,7 @@ main(int argc, char *argv[])
if (info.flush_type != NO_FLUSH && info.flush_type != SYNC_FLUSH &&
info.flush_type != FULL_FLUSH) {
printf("Unsupported flush type\n");
exit(0);
exit(1);
}
break;
@@ -790,13 +790,13 @@ main(int argc, char *argv[])
in = fopen(info.file_name, "rb");
if (NULL == in) {
printf("Error: Can not find file %s\n", info.file_name);
exit(0);
exit(1);
}
info.file_size = get_filesize(in);
if (info.file_size == 0) {
printf("Error: input file has 0 size\n");
exit(0);
exit(1);
}
decompbuf_size = info.file_size;
@@ -813,7 +813,7 @@ main(int argc, char *argv[])
filebuf = malloc(info.file_size);
if (filebuf == NULL) {
fprintf(stderr, "Can't allocate temp buffer memory\n");
exit(0);
exit(1);
}
block_count = 1;
@@ -829,18 +829,18 @@ main(int argc, char *argv[])
compressbuf = malloc(compressbuf_size);
if (compressbuf == NULL) {
fprintf(stderr, "Can't allocate input buffer memory\n");
exit(0);
exit(1);
}
decompbuf = malloc(decompbuf_size);
if (decompbuf == NULL) {
fprintf(stderr, "Can't allocate output buffer memory\n");
exit(0);
exit(1);
}
if (info.file_size != fread(filebuf, 1, info.file_size, in)) {
fprintf(stderr, "Could not read in all input\n");
exit(0);
exit(1);
}
fclose(in);
@@ -891,7 +891,7 @@ main(int argc, char *argv[])
if (out == NULL) {
fprintf(stderr, "Could not write to the output file \"%s\"\n",
outfile);
exit(0);
exit(1);
}
fwrite(compressbuf, 1, info.deflate_size, out);
fclose(out);

View File

@@ -160,7 +160,7 @@ usage(void)
" -r <iter> number of randoms for each test\n"
" -s <seed> set rand() test seed\n"
" -v enable verbose test log\n");
exit(0);
exit(1);
}
size_t
@@ -657,7 +657,7 @@ inflate_multi_pass(uint8_t *compress_buf, uint64_t compress_len, uint8_t *uncomp
state = malloc(sizeof(struct inflate_state));
if (state == NULL) {
printf("Failed to allocate memory\n");
exit(0);
exit(1);
}
create_rand_repeat_data((uint8_t *) state, sizeof(*state));

View File

@@ -56,7 +56,7 @@ usage(void)
" -s <size> sample size default=%d\n"
" -o <file> output file\n",
DEFAULT_SEG_SIZE, DEFAULT_SAMPLE_SIZE);
exit(0);
exit(1);
}
int
@@ -241,19 +241,23 @@ main(int argc, char *argv[])
*/
infile_size = get_filesize(in);
if (infile_size == 0) {
printf("Input file has zero length\n");
usage();
/* Check if it's a real error or just an empty file */
if (fseek(in, 0, SEEK_END) != 0 || ftell(in) < 0)
fprintf(stderr, "Failed to get file size\n");
else
fprintf(stderr, "Input file has zero length\n");
exit(1);
}
outbuf_size = infile_size * 1.30 > MIN_BUF_SIZE ? infile_size * 1.30 : MIN_BUF_SIZE;
if (NULL == (inbuf = malloc(infile_size))) {
fprintf(stderr, "Can't allocate input buffer memory\n");
exit(0);
exit(1);
}
if (NULL == (outbuf = malloc(outbuf_size))) {
fprintf(stderr, "Can't allocate output buffer memory\n");
exit(0);
exit(1);
}
int hist_size = sample_size > segment_size ? segment_size : sample_size;
@@ -266,7 +270,7 @@ main(int argc, char *argv[])
stream.avail_in = (uint32_t) fread(inbuf, 1, infile_size, in);
if (stream.avail_in != infile_size) {
fprintf(stderr, "Couldn't fit all of input file into buffer\n");
exit(0);
exit(1);
}
struct perf start;
@@ -308,7 +312,7 @@ main(int argc, char *argv[])
if (NULL == (inflate_buf = malloc(infile_size))) {
fprintf(stderr, "Can't allocate reconstruct buffer memory\n");
exit(0);
exit(1);
}
isal_inflate_init(&istate);
istate.next_in = outbuf;

View File

@@ -44,17 +44,17 @@ main(int argc, char *argv[])
if (argc != 3) {
fprintf(stderr, "Usage: igzip_sync_flush_example infile outfile\n");
exit(0);
exit(1);
}
in = fopen(argv[1], "rb");
if (!in) {
fprintf(stderr, "Can't open %s for reading\n", argv[1]);
exit(0);
exit(1);
}
out = fopen(argv[2], "wb");
if (!out) {
fprintf(stderr, "Can't open %s for writing\n", argv[2]);
exit(0);
exit(1);
}
printf("igzip_sync_flush_example\nWindow Size: %d K\n", IGZIP_HIST_SIZE / 1024);