Skip to content

Commit 974884e

Browse files
Prevent dereference and add const to bounds pointers during encode
PiperOrigin-RevId: 762591913
1 parent a7f2770 commit 974884e

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

upb/mem/internal/arena.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
struct upb_Arena {
3030
char* UPB_ONLYBITS(ptr);
31-
char* UPB_ONLYBITS(end);
31+
const UPB_NODEREF char* UPB_ONLYBITS(end);
3232
UPB_XSAN_MEMBER
3333
};
3434

upb/port/def.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ Error, UINTPTR_MAX is undefined
235235
#define UPB_PRINTF(str, first_vararg)
236236
#endif
237237

238+
#if defined(__clang__)
239+
#define UPB_NODEREF __attribute__((noderef))
240+
#else
241+
#define UPB_NODEREF
242+
#endif
243+
238244
#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
239245
#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
240246

upb/port/undef.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#undef UPB_NOINLINE
3131
#undef UPB_NORETURN
3232
#undef UPB_PRINTF
33+
#undef UPB_NODEREF
3334
#undef UPB_MAX
3435
#undef UPB_MIN
3536
#undef UPB_UNUSED

upb/wire/encode.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ typedef struct {
6464
upb_EncodeStatus status;
6565
jmp_buf err;
6666
upb_Arena* arena;
67-
char *buf, *limit;
67+
// These should only be used for arithmetic and reallocation to allow full
68+
// aliasing analysis on the ptr argument.
69+
const char UPB_NODEREF *buf, *limit;
6870
int options;
6971
int depth;
7072
_upb_mapsorter sorter;
@@ -89,7 +91,8 @@ static char* encode_growbuffer(char* ptr, upb_encstate* e, size_t bytes) {
8991
size_t old_size = e->limit - e->buf;
9092
size_t needed_size = bytes + (e->limit - ptr);
9193
size_t new_size = upb_roundup_pow2(needed_size);
92-
char* new_buf = upb_Arena_Realloc(e->arena, e->buf, old_size, new_size);
94+
char* new_buf =
95+
upb_Arena_Realloc(e->arena, (void*)e->buf, old_size, new_size);
9396

9497
if (!new_buf) encode_err(e, kUpb_EncodeStatus_OutOfMemory);
9598

0 commit comments

Comments
 (0)