Switch to a macro to avoid signed/const mismatches.

pull/13171/head
Joshua Haberman 4 years ago
parent 63ad3db980
commit 8370818143
  1. 13
      upb/json_encode.c
  2. 3
      upb/port_def.inc
  3. 11
      upb/port_undef.inc
  4. 10
      upb/text_encode.c

@ -35,11 +35,6 @@ static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
const upb_msgdef *m, bool first);
static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
static char *jsonenc_ptradd(char *ptr, size_t size) {
// Generates the same code as "ptr + size" but avoids UB of NULL + 0.
return size ? ptr + size : ptr;
}
UPB_NORETURN static void jsonenc_err(jsonenc *e, const char *msg) {
upb_status_seterrmsg(e->status, msg);
longjmp(e->err, 1);
@ -93,7 +88,7 @@ static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
if (have) e->ptr += have;
e->ptr = UPB_PTRADD(e->ptr, have);
e->overflow += (n - have);
}
}
@ -197,7 +192,7 @@ static void jsonenc_bytes(jsonenc *e, upb_strview str) {
static const char base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const unsigned char *ptr = (unsigned char*)str.data;
const unsigned char *end = jsonenc_ptradd(ptr, str.size);
const unsigned char *end = UPB_PTRADD(ptr, str.size);
char buf[4];
jsonenc_putstr(e, "\"");
@ -233,7 +228,7 @@ static void jsonenc_bytes(jsonenc *e, upb_strview str) {
static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
const char *ptr = str.data;
const char *end = jsonenc_ptradd(ptr, str.size);
const char *end = UPB_PTRADD(ptr, str.size);
while (ptr < end) {
switch (*ptr) {
@ -705,7 +700,7 @@ size_t upb_json_encode(const upb_msg *msg, const upb_msgdef *m,
e.buf = buf;
e.ptr = buf;
e.end = jsonenc_ptradd(buf, size);
e.end = UPB_PTRADD(buf, size);
e.overflow = 0;
e.options = options;
e.ext_pool = ext_pool;

@ -135,6 +135,9 @@
#define UPB_LONGJMP(buf, val) longjmp(buf, val)
#endif
/* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */
#define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr))
/* Configure whether fasttable is switched on or not. *************************/
#if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__)

@ -1,24 +1,33 @@
/* See port_def.inc. This should #undef all macros #defined there. */
#undef UPB_MAPTYPE_STRING
#undef UPB_SIZE
#undef UPB_PTR_AT
#undef UPB_READ_ONEOF
#undef UPB_WRITE_ONEOF
#undef UPB_MAPTYPE_STRING
#undef UPB_INLINE
#undef UPB_ALIGN_UP
#undef UPB_ALIGN_DOWN
#undef UPB_ALIGN_MALLOC
#undef UPB_ALIGN_OF
#undef UPB_LIKELY
#undef UPB_UNLIKELY
#undef UPB_FORCEINLINE
#undef UPB_NOINLINE
#undef UPB_NORETURN
#undef UPB_PRINTF
#undef UPB_MAX
#undef UPB_MIN
#undef UPB_UNUSED
#undef UPB_ASSUME
#undef UPB_ASSERT
#undef UPB_UNREACHABLE
#undef UPB_SETJMP
#undef UPB_LONGJMP
#undef UPB_PTRADD
#undef UPB_FASTTABLE_SUPPORTED
#undef UPB_FASTTABLE
#undef UPB_FASTTABLE_INIT
#undef UPB_POISON_MEMORY_REGION
#undef UPB_UNPOISON_MEMORY_REGION
#undef UPB_ASAN

@ -28,8 +28,10 @@ static void txtenc_putbytes(txtenc *e, const void *data, size_t len) {
memcpy(e->ptr, data, len);
e->ptr += len;
} else {
if (have) memcpy(e->ptr, data, have);
e->ptr += have;
if (have) {
memcpy(e->ptr, data, have);
e->ptr += have;
}
e->overflow += (len - have);
}
}
@ -50,7 +52,7 @@ static void txtenc_printf(txtenc *e, const char *fmt, ...) {
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
e->ptr += have;
e->ptr = UPB_PTRADD(e->ptr, have);
e->overflow += (n - have);
}
}
@ -408,7 +410,7 @@ size_t upb_text_encode(const upb_msg *msg, const upb_msgdef *m,
e.buf = buf;
e.ptr = buf;
e.end = buf + size;
e.end = UPB_PTRADD(buf, size);
e.overflow = 0;
e.indent_depth = 0;
e.options = options;

Loading…
Cancel
Save