Dynamically allocate string for error msg.

pull/13171/head
Joshua Haberman 15 years ago
parent be5ddd8a64
commit b04ff41664
  1. 12
      src/upb.c
  2. 12
      src/upb.h

@ -10,6 +10,7 @@
#include <string.h> #include <string.h>
#include "upb.h" #include "upb.h"
#include "upb_string.h"
#define alignof(t) offsetof(struct { char c; t x; }, x) #define alignof(t) offsetof(struct { char c; t x; }, x)
#define TYPE_INFO(wire_type, ctype, allows_delimited) \ #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, ...) const char *msg, ...)
{ {
if(upb_ok(status)) { // The first error is the most interesting. 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; status->code = code;
va_list args; va_list args;
va_start(args, msg); va_start(args, msg);
vsnprintf(status->msg, UPB_ERRORMSG_MAXLEN, msg, args); vsnprintf(str, UPB_ERRORMSG_MAXLEN, msg, args);
va_end(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) void upb_copyerr(upb_status *to, upb_status *from)
{ {
to->code = from->code; 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;
}

@ -281,23 +281,19 @@ enum upb_status_code {
UPB_ERROR_MAX_NESTING_EXCEEDED = -3 UPB_ERROR_MAX_NESTING_EXCEEDED = -3
}; };
#define UPB_ERRORMSG_MAXLEN 256
typedef struct { typedef struct {
enum upb_status_code code; enum upb_status_code code;
char msg[UPB_ERRORMSG_MAXLEN]; upb_string *str;
} upb_status; } 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) { INLINE bool upb_ok(upb_status *status) {
return status->code == UPB_STATUS_OK; return status->code == UPB_STATUS_OK;
} }
INLINE void upb_reset(upb_status *status) { void upb_reset(upb_status *status);
status->code = UPB_STATUS_OK;
status->msg[0] = '\0';
}
void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg, void upb_seterr(upb_status *status, enum upb_status_code code, const char *msg,
...); ...);
void upb_copyerr(upb_status *to, upb_status *from); void upb_copyerr(upb_status *to, upb_status *from);

Loading…
Cancel
Save