From c17158f059ed075cbac2fd6e6b3be8ea9ba5de6c Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 22 May 2010 11:28:18 -0700 Subject: [PATCH] dissect-sysfile: Allow padding compressed data at end of file without error. Compressed data in a system file can end either with a special opcode or by padding out the last set of opcodes to an 8-byte boundary with null bytes. Until now, dissect-sysfile has complained about last with "unexpected end of file". This commit fixes the problem. --- tests/dissect-sysfile.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/dissect-sysfile.c b/tests/dissect-sysfile.c index 4fc610a9..6aff2650 100644 --- a/tests/dissect-sysfile.c +++ b/tests/dissect-sysfile.c @@ -102,6 +102,7 @@ static void sys_error (struct sfm_reader *, const char *, ...) NO_RETURN; static void read_bytes (struct sfm_reader *, void *, size_t); +static bool try_read_bytes (struct sfm_reader *, void *, size_t); static int read_int (struct sfm_reader *); static int64_t read_int64 (struct sfm_reader *); static double read_float (struct sfm_reader *); @@ -1093,7 +1094,13 @@ read_compressed_data (struct sfm_reader *r) if (opcode_idx >= N_OPCODES) { opcode_ofs = ftello (r->file); - read_bytes (r, opcodes, 8); + if (i == 0) + { + if (!try_read_bytes (r, opcodes, 8)) + return; + } + else + read_bytes (r, opcodes, 8); opcode_idx = 0; } opcode = opcodes[opcode_idx]; @@ -1360,6 +1367,16 @@ read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt) read_bytes_internal (r, false, buf, byte_cnt); } +/* Reads BYTE_CNT bytes into BUF. + Returns true if exactly BYTE_CNT bytes are successfully read. + Returns false if an immediate end-of-file is encountered. + Aborts if an I/O error or a partial read occurs. */ +static bool +try_read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt) +{ + return read_bytes_internal (r, true, buf, byte_cnt); +} + /* Reads a 32-bit signed integer from R and returns its value in host format. */ static int -- 2.30.2