Skip to contentSkip to conversation

replay: Improve assert in replay_char_read_all_load()

PATCH Applied Peter Maydell 2 participants 3 messages Nov 24, 2025 – Nov 25, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 replay: Improve assert in replay_char_read_all_load()

In replay_char_read_all_load() we get a buffer and size from the replay log. We know the size has to fit an int because of how we write the log. However the way we assert this is wrong: we cast the size_t from replay_get_array() to an int and then check that it is non-negative. This misses cases where an over-large size is truncated into a positive value by the cast.

Replace the assertion with checking that the size is in-range before doing the cast.

Coverity complained about the possible overflow: CID 1643440.

---
 replay/replay-char.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
replay/replay-char.c +1 -1
index 81dc416e988..4e58dd154af 100644
@@ -126,8 +126,8 @@ int replay_char_read_all_load(uint8_t *buf)
         int res;
         replay_get_array(buf, &size);
         replay_finish_event();
+        assert(size <= INT_MAX);
         res = (int)size;
-        assert(res >= 0);
         return res;
     } else if (replay_next_event_is(EVENT_CHAR_READ_ALL_ERROR)) {
         int res = replay_get_dword();
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Peter Maydell

In replay_char_read_all_load() we get a buffer and size from the replay log. We know the size has to fit an int because of how we write the log. However the way we assert this is wrong: we cast the size_t from replay_get_array() to an int and then check that it is non-negative. This misses cases where an over-large size is truncated into a positive value by the cast.

Replace the assertion with checking that the size is in-range before doing the cast.

Quoted 6 lines

Coverity complained about the possible overflow: CID 1643440.

---
  replay/replay-char.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#2 lore raw
In reply to Peter Maydell
Quoted 10 lines · Peter Maydell

In replay_char_read_all_load() we get a buffer and size from the replay log. We know the size has to fit an int because of how we write the log. However the way we assert this is wrong: we cast the size_t from replay_get_array() to an int and then check that it is non-negative. This misses cases where an over-large size is truncated into a positive value by the cast.

Replace the assertion with checking that the size is in-range before doing the cast.

Quoted 6 lines

Coverity complained about the possible overflow: CID 1643440.

---
  replay/replay-char.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

Queued, thanks.