From b04ff41664f45c3c86eea62173a1223ff04d6ad7 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 9 Jul 2010 19:56:58 -0700 Subject: [PATCH] Dynamically allocate string for error msg. --- src/upb.c | 12 ++++++++++-- src/upb.h | 12 ++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/upb.c b/src/upb.c index 3c5efe8cbb..a98512dca3 100644 --- a/src/upb.c +++ b/src/upb.c @@ -10,6 +10,7 @@ #include #include "upb.h" +#include "upb_string.h" #define alignof(t) offsetof(struct { char c; t x; }, x) #define TYPE_INFO(wire_type, ctype, allows_delimited) \ @@ -43,10 +44,12 @@ void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg, ...) { if(upb_ok(status)) { // The first error is the most interesting. + status->str = upb_string_new(); + char *str = upb_string_getrwbuf(status->str, UPB_ERRORMSG_MAXLEN); status->code = code; va_list args; va_start(args, msg); - vsnprintf(status->msg, UPB_ERRORMSG_MAXLEN, msg, args); + vsnprintf(str, UPB_ERRORMSG_MAXLEN, msg, args); va_end(args); } } @@ -54,6 +57,11 @@ void upb_seterr(upb_status *status, enum upb_status_code code, void upb_copyerr(upb_status *to, upb_status *from) { to->code = from->code; - strcpy(to->msg, from->msg); + to->str = upb_string_getref(from->str); } +void upb_reset(upb_status *status) { + status->code = UPB_STATUS_OK; + upb_string_unref(status->str); + status->str = NULL; +} diff --git a/src/upb.h b/src/upb.h index 6bf548c27f..6502dfce20 100644 --- a/src/upb.h +++ b/src/upb.h @@ -281,23 +281,19 @@ enum upb_status_code { UPB_ERROR_MAX_NESTING_EXCEEDED = -3 }; -#define UPB_ERRORMSG_MAXLEN 256 typedef struct { enum upb_status_code code; - char msg[UPB_ERRORMSG_MAXLEN]; + upb_string *str; } upb_status; -#define UPB_STATUS_INIT {UPB_STATUS_OK, ""} +#define UPB_STATUS_INIT {UPB_STATUS_OK, NULL} +#define UPB_ERRORMSG_MAXLEN 256 INLINE bool upb_ok(upb_status *status) { return status->code == UPB_STATUS_OK; } -INLINE void upb_reset(upb_status *status) { - status->code = UPB_STATUS_OK; - status->msg[0] = '\0'; -} - +void upb_reset(upb_status *status); void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg, ...); void upb_copyerr(upb_status *to, upb_status *from);