diff --git a/upb/bytestream.c b/upb/bytestream.c index 4f6e4b104c..812e55226d 100644 --- a/upb/bytestream.c +++ b/upb/bytestream.c @@ -32,6 +32,8 @@ upb_byteregion *upb_byteregion_newl(const void *str, size_t len) { memcpy(ptr, str, len); ptr[len] = '\0'; upb_stringsrc_reset(src, ptr, len); + upb_byteregion_fetch(upb_stringsrc_allbytes(src)); + assert(len == upb_byteregion_available(upb_stringsrc_allbytes(src), 0)); return upb_stringsrc_allbytes(src); } @@ -241,9 +243,10 @@ upb_bytesink* upb_stdio_bytesink(upb_stdio *stdio) { return &stdio->sink; } upb_bytesuccess_t upb_stringsrc_fetch(void *_src, uint64_t ofs, size_t *read) { upb_stringsrc *src = _src; - assert(ofs < src->len); + assert(ofs <= src->len); if (ofs == src->len) { upb_status_seteof(&src->bytesrc.status); + *read = 0; return UPB_BYTE_EOF; } *read = src->len - ofs; diff --git a/upb/def.c b/upb/def.c index 246e9bb7a0..5ac3498868 100644 --- a/upb/def.c +++ b/upb/def.c @@ -235,6 +235,7 @@ upb_fielddef *upb_fielddef_new() { } static void upb_fielddef_init_default(upb_fielddef *f) { + f->default_is_string = false; switch (upb_fielddef_type(f)) { case UPB_TYPE(DOUBLE): upb_value_setdouble(&f->defaultval, 0); break; case UPB_TYPE(FLOAT): upb_value_setfloat(&f->defaultval, 0); break; @@ -252,15 +253,16 @@ static void upb_fielddef_init_default(upb_fielddef *f) { case UPB_TYPE(BOOL): upb_value_setbool(&f->defaultval, false); break; case UPB_TYPE(STRING): case UPB_TYPE(BYTES): - upb_value_setbyteregion(&f->defaultval, upb_byteregion_new("")); break; + f->default_is_string = true; + upb_value_setbyteregion(&f->defaultval, upb_byteregion_new("")); + break; case UPB_TYPE(GROUP): case UPB_TYPE(MESSAGE): upb_value_setptr(&f->defaultval, NULL); break; } - f->default_is_symbolic = false; } static void upb_fielddef_uninit_default(upb_fielddef *f) { - if (upb_isstring(f) || f->default_is_symbolic) { + if (f->default_is_string) { upb_byteregion_free(upb_value_getbyteregion(f->defaultval)); } } @@ -323,7 +325,7 @@ static bool upb_fielddef_resolve(upb_fielddef *f, upb_def *def, upb_status *s) { assert(upb_dyncast_unresolveddef(f->def)); upb_def_unref(f->def); f->def = def; - if (f->type == UPB_TYPE(ENUM) && f->default_is_symbolic) { + if (f->type == UPB_TYPE(ENUM) && f->default_is_string) { // Resolve the enum's default from a string to an integer. upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); assert(bytes); // Points to either a real default or the empty string. @@ -347,6 +349,7 @@ static bool upb_fielddef_resolve(upb_fielddef *f, upb_def *def, upb_status *s) { } upb_value_setint32(&f->defaultval, val); } + f->default_is_string = false; upb_byteregion_free(bytes); } return true; @@ -387,11 +390,13 @@ void upb_fielddef_setdefault(upb_fielddef *f, upb_value value) { void upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len) { assert(upb_isstring(f) || f->type == UPB_TYPE(ENUM)); - upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); - assert(bytes); - upb_byteregion_free(bytes); + if (f->default_is_string) { + upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); + assert(bytes); + upb_byteregion_free(bytes); + } upb_value_setbyteregion(&f->defaultval, upb_byteregion_newl(str, len)); - f->default_is_symbolic = true; + f->default_is_string = true; } void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str) { diff --git a/upb/def.h b/upb/def.h index 67e478b9b7..462655a5fe 100644 --- a/upb/def.h +++ b/upb/def.h @@ -109,7 +109,7 @@ typedef struct _upb_fielddef { uint8_t label; // Use UPB_LABEL() constants. int16_t hasbit; uint16_t offset; - bool default_is_symbolic; + bool default_is_string; bool active; int32_t number; char *name; @@ -161,7 +161,7 @@ INLINE upb_value upb_fielddef_default(const upb_fielddef *f) { // returns true, the default returned from upb_fielddef_default() is a string, // otherwise it is an integer. INLINE bool upb_fielddef_default_is_symbolic(const upb_fielddef *f) { - return f->default_is_symbolic; + return f->default_is_string; } // The enum or submessage def for this field, if any. Only meaningful for