Skip to content

Commit fbc972b

Browse files
authored
Fix compressed size calculation for the zstd condition (#18420)
There was a variable shadowing issue. I also fixed a few clang analyzer warnings.
1 parent 97dea65 commit fbc972b

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

input/bsv/bsvmovie.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "bsvmovie.h"
1919
#include <retro_endianness.h>
20+
#include <stdint.h>
2021
#include "../input_driver.h"
2122
#include "../../retroarch.h"
2223
#include "../../state_manager.h"
@@ -144,7 +145,8 @@ static bool bsv_movie_peek_frame_info(bsv_movie_t *movie, uint8_t *token, uint64
144145
if (intfstream_read(movie->file, &(state_length), sizeof(uint64_t)) != sizeof(uint64_t))
145146
goto end;
146147
state_length = swap_if_big64(state_length);
147-
ret = intfstream_seek(movie->file, state_length, SEEK_CUR) >= 0;
148+
if (intfstream_seek(movie->file, state_length, SEEK_CUR) < 0)
149+
goto end;
148150
}
149151
else if (tok == REPLAY_TOKEN_CHECKPOINT2_FRAME)
150152
{
@@ -156,7 +158,8 @@ static bool bsv_movie_peek_frame_info(bsv_movie_t *movie, uint8_t *token, uint64
156158
if (intfstream_read(movie->file, &(state_length), sizeof(uint32_t)) != sizeof(uint32_t))
157159
goto end;
158160
/* Seek past the state data */
159-
ret = intfstream_seek(movie->file, state_length, SEEK_CUR) >= 0;
161+
if (intfstream_seek(movie->file, state_length, SEEK_CUR) < 0)
162+
goto end;
160163
}
161164
/* We are already at the end of the frame */
162165
else if (tok == REPLAY_TOKEN_REGULAR_FRAME) { }
@@ -668,7 +671,7 @@ int64_t bsv_movie_write_checkpoint(bsv_movie_t *handle, uint8_t compression, uin
668671
uint8_t *swap;
669672
size_t size_swap;
670673
int64_t ret = -1;
671-
uint32_t encoded_size = 0, compressed_encoded_size = 0, size_ = 0;
674+
uint32_t encoded_size = 0, compressed_encoded_size, size_ = 0;
672675
uint8_t *encoded_data = NULL, *compressed_encoded_data = NULL;
673676
bool owns_encoded = false, owns_compressed_encoded = false;
674677
retro_ctx_serialize_info_t serial_info;
@@ -732,17 +735,17 @@ int64_t bsv_movie_write_checkpoint(bsv_movie_t *handle, uint8_t compression, uin
732735
#ifdef HAVE_ZSTD
733736
case REPLAY_CHECKPOINT2_COMPRESSION_ZSTD:
734737
{
735-
size_t compressed_encoded_size = ZSTD_compressBound(encoded_size);
736-
compressed_encoded_data = (uint8_t*)calloc(compressed_encoded_size, sizeof(uint8_t));
738+
size_t compressed_encoded_size_zstd = ZSTD_compressBound(encoded_size);
739+
compressed_encoded_data = (uint8_t*)calloc(compressed_encoded_size_zstd, sizeof(uint8_t));
737740
owns_compressed_encoded = true;
738-
compressed_encoded_size = ZSTD_compress(compressed_encoded_data, compressed_encoded_size, encoded_data, encoded_size, 3);
739-
if (ZSTD_isError(compressed_encoded_size))
741+
compressed_encoded_size_zstd = ZSTD_compress(compressed_encoded_data, compressed_encoded_size_zstd, encoded_data, encoded_size, 3);
742+
if (ZSTD_isError(compressed_encoded_size_zstd))
740743
{
741744
ret = -1;
742745
goto exit;
743746
}
744747
/* Have to cast after checking the error flags, not before */
745-
compressed_encoded_size = (uint32_t)compressed_encoded_size;
748+
compressed_encoded_size = (uint32_t)compressed_encoded_size_zstd;
746749
break;
747750
}
748751
#endif
@@ -1207,11 +1210,13 @@ bool replay_check_same_timeline(bsv_movie_t *movie,
12071210
intfstream_seek(movie->file, sizeof(uint32_t), SEEK_CUR);
12081211
intfstream_seek(check_stream, sizeof(uint32_t), SEEK_CUR);
12091212
}
1213+
keycount1 = UINT8_MAX;
1214+
keycount2 = UINT8_MAX;
12101215
if (intfstream_read(movie->file, &keycount1, 1) < 1 ||
12111216
intfstream_read(check_stream, &keycount2, 1) < 1 ||
12121217
keycount1 != keycount2)
12131218
{
1214-
RARCH_ERR("[Replay] Replay checkpoints disagree on key count, %d vs %d\n", keycount1, keycount2);
1219+
RARCH_ERR("[Replay] Replay checkpoints disagree on key count, %d vs %d (255 means read error)\n", keycount1, keycount2);
12151220
ret = false;
12161221
goto exit;
12171222
}
@@ -1658,6 +1663,10 @@ bool bsv_movie_read_deduped_state(bsv_movie_t *movie, uint8_t *encoded, size_t e
16581663
free(movie->superblock_seq);
16591664
movie->superblock_seq = NULL;
16601665
}
1666+
if (!movie->cur_save) {
1667+
RARCH_ERR("[STATESTREAM] movie has no current serialized save\n");
1668+
goto exit;
1669+
}
16611670
total_decode_count++;
16621671
rmsgpack_dom_read_with(read_mem, &item, reader_state);
16631672
if (item.type != RDT_INT && item.type != RDT_UINT)

input/bsv/uint32s_index.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define XXH_INLINE_ALL
2626
#include <xxHash/xxhash.h>
2727

28-
#define HASHMAP_CAP (1 << 16)
28+
#define HASHMAP_CAP 65536
2929
#define uint32s_hash_bytes(bytes, len) XXH32(bytes,len,0)
3030

3131
uint32s_index_t *uint32s_index_new(size_t object_size,
@@ -283,11 +283,16 @@ void uint32s_index_pop(uint32s_index_t *index)
283283
uint32_t idx = RBUF_LEN(index->objects)-1;
284284
uint32_t hash = index->hashes[idx];
285285
struct uint32s_bucket *bucket = RHMAP_PTR(index->index, hash);
286+
uint32_t old_len = bucket->len;
287+
if (old_len == 0) {
288+
RARCH_ERR("[STATESTREAM] trying to pop from empty bucket\n");
289+
return;
290+
}
286291
RBUF_RESIZE(index->objects, idx);
287292
RBUF_RESIZE(index->counts, idx);
288293
RBUF_RESIZE(index->hashes, idx);
289294
uint32s_bucket_remove(bucket, idx);
290-
if (bucket->len == 0)
295+
if (old_len - 1 == 0)
291296
{
292297
uint32s_bucket_free(bucket);
293298
if (!RHMAP_DEL(index->index, hash))

0 commit comments

Comments
 (0)