diff options
author | EuAndreh <eu@euandre.org> | 2024-05-24 22:08:02 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-05-24 22:10:24 -0300 |
commit | 8ebb7b875a0bc5a417ce62238afcc79c366b5fa1 (patch) | |
tree | 13a37aaad7f11831196edfbdede27083eb0bd4fe /src/catalog.c | |
parent | src/i18n.c: Remove leading empty string in MSGS[] (diff) | |
download | pindaiba-8ebb7b875a0bc5a417ce62238afcc79c366b5fa1.tar.gz pindaiba-8ebb7b875a0bc5a417ce62238afcc79c366b5fa1.tar.xz |
src/catalog.h: Do bounds checking on calls to s()
Diffstat (limited to 'src/catalog.c')
-rw-r--r-- | src/catalog.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/catalog.c b/src/catalog.c index 3e40bf2..9550631 100644 --- a/src/catalog.c +++ b/src/catalog.c @@ -11,12 +11,16 @@ #include "catalog.h" + static const char *const CATALOG_NAME = NAME; static nl_catd catalog_descriptor = NULL; +static size_t +msgs_length = 0U; + static const char *const NLSPATH = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" LOCALEDIR "/%l/LC_MESSAGES/%N.cat"; @@ -26,7 +30,7 @@ NLSPATH_KEY = "NLSPATH"; int -i18n_init(void) { +i18n_init_msgs(const char *const MSGS[]) { int rc = -1; static const int should_overwrite = 0; @@ -42,6 +46,11 @@ i18n_init(void) { goto out; } + msgs_length = 0U; + while (MSGS[msgs_length] != NULL) { + msgs_length++; + } + rc = 0; out: return rc; @@ -66,14 +75,12 @@ out: return rc; } -/** - * Infallible: always returns a valid string, no matter what. - */ const char * s(const char* const MSGS[], const int msg_id) { - assert(msg_id > 0); - // FIXME: assert within bounds! - // printf("sizeof(MSGS): %ld\n", sizeof(MSGS)); + assert(msg_id >= 0); + if (msgs_length != 0U) { + assert((size_t)msg_id < msgs_length); + } if (catalog_descriptor == NULL) { return MSGS[msg_id]; } |