From be643d66b1c3f0673bbf4c9379ab47c806895187 Mon Sep 17 00:00:00 2001 From: krcroft Date: Thu, 5 Mar 2020 16:00:05 -0800 Subject: [PATCH] Cleanup file size and header length check --- src/libs/decoders/mp3_seek_table.cpp | 17 ++++++++--------- src/libs/decoders/opus.cpp | 6 ++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/libs/decoders/mp3_seek_table.cpp b/src/libs/decoders/mp3_seek_table.cpp index e5a6c886..cf70d69b 100644 --- a/src/libs/decoders/mp3_seek_table.cpp +++ b/src/libs/decoders/mp3_seek_table.cpp @@ -100,13 +100,13 @@ using std::ofstream; // time point. The trade-off is as follows: // - a large number means slower in-game seeking but a smaller fast-seek file. // - a smaller numbers (below 10) results in fast seeks on slow hardware. -#define FRAMES_PER_SEEK_POINT 7u +constexpr uint32_t FRAMES_PER_SEEK_POINT = 7; -// Returns the size of a file in bytes (if valid), otherwise 0 -uint64_t get_file_size(const char* filename) { +// Returns the size of a file in bytes (if valid), otherwise -1 +off_t get_file_size(const char* filename) { struct stat stat_buf; int rc = stat(filename, &stat_buf); - return rc == 0 ? static_cast(stat_buf.st_size) : 0u; + return (rc >= 0) ? stat_buf.st_size : -1; } @@ -261,18 +261,17 @@ Uint64 load_existing_seek_points(const char* filename, return 0; } - // Sentinal 2: Bail if the file isn't even big enough to hold our 4-byte header string. - const string expected_identifier(SEEK_TABLE_IDENTIFIER); - if (get_file_size(filename) < 4 + expected_identifier.length()) { + // Sentinal 2: Bail if the file isn't big enough to hold our identifier. + if (get_file_size(filename) < static_cast(sizeof(SEEK_TABLE_IDENTIFIER))) { return 0; } - // Sentinal 3: Bail if we don't get a match on our ID string. + // Sentinal 3: Bail if we don't get a matching identifier. string fetched_identifier; ifstream infile(filename, ios_base::binary); Archive deserialize(infile); deserialize >> fetched_identifier; - if (fetched_identifier != expected_identifier) { + if (fetched_identifier != SEEK_TABLE_IDENTIFIER) { infile.close(); return 0; } diff --git a/src/libs/decoders/opus.cpp b/src/libs/decoders/opus.cpp index 3c188a26..d2da0532 100644 --- a/src/libs/decoders/opus.cpp +++ b/src/libs/decoders/opus.cpp @@ -331,7 +331,7 @@ static int32_t opus_seek(Sound_Sample * sample, const uint32_t ms) auto *of = static_cast(internal->decoder_private); #if (defined DEBUG_CHATTER) - const float total_seconds = static_cast(ms) / 1000; + const float total_seconds = ms / 1000.0; uint8_t minutes = total_seconds / 60; const double seconds = static_cast(total_seconds) % 60 @@ -365,9 +365,7 @@ static int32_t opus_rewind(Sound_Sample* sample) { // Guard against invalid input assertm(sample, "OPUS: Input is not initialized"); - const int32_t rcode = opus_seek(sample, 0); - assertm(rcode >= 0, "OPUS: seek failed [bug or corrupt Opus track]"); - return rcode; + return opus_seek(sample, 0); } /* opus_rewind */