]> git.draconx.ca Git - cdecl99.git/blobdiff - src/error.c
libcdecl: Sidestep some possible snprintf issues.
[cdecl99.git] / src / error.c
index fe0695c263ab9e45786b2a75b597bf1a4657c963..cf4411e0e89b8da7dfbe82fcbda7ddf0c18c871b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Error handling for libcdecl.
- *  Copyright © 2011-2012, 2021 Nick Bowler
+ *  Copyright © 2011-2012, 2021, 2023 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -19,9 +19,7 @@
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <assert.h>
-#include <stdarg.h>
 
 #include "cdecl.h"
 #include "cdecl-internal.h"
@@ -29,8 +27,7 @@
 #include <glthread/lock.h>
 #include <glthread/tls.h>
 
-gl_once_define(static, tls_initialized);
-static gl_tls_key_t tls_key;
+#include "errmsg.h"
 
 struct err_state {
        struct cdecl_error err;
@@ -38,13 +35,9 @@ struct err_state {
        char str[FLEXIBLE_ARRAY_MEMBER];
 };
 
-/* This error is reserved for extremely dire out-of-memory conditions. */
-static struct err_state err_no_mem = {
-       .err = {
-               .code = CDECL_ENOMEM,
-               .str  = NULL,
-       },
-};
+/* This pre-initialized error is reserved for dire out-of-memory conditions. */
+static struct cdecl_error err_no_mem;
+static gl_tls_key_t tls_key;
 
 static void free_err(void *err)
 {
@@ -54,71 +47,115 @@ static void free_err(void *err)
        free(err);
 }
 
-static void initialize(void)
+static void set_err(unsigned code, struct cdecl_error *err)
 {
-       cdecl__init_i18n();
-       err_no_mem.err.str = _("failed to allocate memory");
+       static const char errmsgs[] = STRTAB_INITIALIZER;
+
+       switch (code) {
+       case CDECL__ENOMEM:
+               err->code = CDECL_ENOMEM;
+               break;
+       default:
+               err->code = CDECL_ENOPARSE;
+               break;
+       }
+
+       err->str = _(&errmsgs[code]);
+}
 
+static void initialize_cb(void)
+{
+#if ENABLE_NLS
+       bindtextdomain(PACKAGE, LOCALEDIR);
+       bindtextdomain("bison-runtime", BISON_LOCALEDIR);
+#endif
+       set_err(CDECL__ENOMEM, &err_no_mem);
        gl_tls_key_init(tls_key, free_err);
 }
 
-/*
- * cdecl__err(CDECL_ENOMEM);
- * cdecl__err(code, fmt, ...);
- *
- * Sets the library error to code, with a printf-style error string.
- */
-void cdecl__err(unsigned code, ...)
+static void *alloc_err_state(void *old, size_t buf_size)
 {
-       const char *fmt;
        struct err_state *state;
-       int rc, try = 0;
-       va_list ap;
+       void *p;
+
+       state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
+       if (state) {
+               state->nstr = buf_size;
+       } else if (old) {
+               /* Failed allocation, but existing state is still good */
+               p = old;
+       } else {
+               /* Failed allocation, no existing state */
+               p = &err_no_mem;
+       }
 
-       gl_once(tls_initialized, initialize);
+       gl_tls_set(tls_key, p);
+       return state;
+}
+
+static struct err_state *get_err_state(void)
+{
+       void *state;
+
+       gl_once_define(static, tls_initialized)
+       gl_once(tls_initialized, initialize_cb);
 
        state = gl_tls_get(tls_key);
-       if (!state || state == &err_no_mem) {
-               void *tmp = malloc(sizeof *state + 100);
+       if (state == &err_no_mem)
+               state = NULL;
+       if (!state)
+               return alloc_err_state(state, 100);
+       return state;
+}
 
-               if (!tmp) {
-                       state = &err_no_mem;
-                       return;
-               }
 
-               gl_tls_set(tls_key, (state = tmp));
-               state->nstr = 100;
-       }
+#if ENABLE_NLS
+/*
+ * Initialize gettext indirectly via get_err_state.
+ */
+void cdecl__init_i18n(void)
+{
+       get_err_state();
+}
+#endif
 
-       if (code == CDECL_ENOMEM) {
-               if (state != &err_no_mem)
-                       state->err = err_no_mem.err;
+/*
+ * Set the library error to one of the preset messages defined in errmsg.h
+ * (CDECL__Exxx).
+ */
+void cdecl__errmsg(unsigned msg)
+{
+       struct err_state *state;
+
+       state = get_err_state();
+       if (!state)
                return;
-       }
-retry:
-       va_start(ap, code);
-       fmt = va_arg(ap, const char *);
-       rc = vsnprintf(state->str, state->nstr, fmt, ap);
-       va_end(ap);
 
-       if (rc > 0 && rc >= state->nstr) {
-               void *tmp;
-               size_t n;
+       set_err(msg, &state->err);
+}
 
-               assert(try == 0 && rc < SIZE_MAX / 4);
+/*
+ * Sets the library error to code; fmt is a printf-style string that may use
+ * up to one %s directive, to refer to arg.
+ */
+void cdecl__err(unsigned code, const char *fmt, const char *arg)
+{
+       struct err_state *state;
+       int rc, try = 0;
 
-               n = ((size_t)rc + 1) * 2;
-               tmp = realloc(state, sizeof *state + n);
-               if (tmp) {
-                       state = tmp;
-                       state->nstr = n;
-                       try++;
+       state = get_err_state();
+       if (!state)
+               return;
 
-                       goto retry;
-               }
+retry:
+       rc = snprintf(state->str, state->nstr, fmt, arg);
+       if (rc > 0 && rc >= state->nstr) {
+               assert(try++ == 0 && rc < SIZE_MAX / 4);
+               state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
+               if (!state)
+                       return;
 
-               state->err = err_no_mem.err;
-               return;
+               goto retry;
        }
 
        state->err.str = state->str;
@@ -127,10 +164,7 @@ retry:
 
 const struct cdecl_error *cdecl_get_error(void)
 {
-       struct err_state *state;
+       struct err_state *state = get_err_state();
 
-       gl_once(tls_initialized, initialize);
-
-       state = gl_tls_get(tls_key);
        return state ? &state->err : NULL;
 }