fix issue #22: add toml_set_memutil

This commit is contained in:
CK Tan 2019-09-26 22:28:23 -07:00
parent 4a31cd8cbf
commit 9394192bfc
2 changed files with 111 additions and 82 deletions

142
toml.c
View file

@ -1,26 +1,28 @@
/* /*
MIT License
Copyright (c) 2017 - 2019 CK Tan MIT License
https://github.com/cktan/tomlc99
Permission is hereby granted, free of charge, to any person obtaining a copy Copyright (c) 2017 - 2019 CK Tan
of this software and associated documentation files (the "Software"), to deal https://github.com/cktan/tomlc99
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all Permission is hereby granted, free of charge, to any person obtaining a copy
copies or substantial portions of the Software. of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/ */
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <stdio.h> #include <stdio.h>
@ -33,11 +35,35 @@ SOFTWARE.
#include <string.h> #include <string.h>
#include "toml.h" #include "toml.h"
static void* (*ppmalloc)(size_t) = malloc;
static void (*ppfree)(void*) = free;
static void* (*ppcalloc)(size_t, size_t) = calloc;
static void* (*pprealloc)(void*, size_t) = realloc;
void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*),
void* (*xxcalloc)(size_t, size_t),
void* (*xxrealloc)(void*, size_t))
{
ppmalloc = xxmalloc;
ppfree = xxfree;
ppcalloc = xxcalloc;
pprealloc = xxrealloc;
}
#define MALLOC(a) ppmalloc(a)
#define FREE(a) ppfree(a)
#define CALLOC(a,b) ppcalloc(a,b)
#define REALLOC(a,b) pprealloc(a,b)
#ifdef _WIN32 #ifdef _WIN32
char* strndup(const char* s, size_t n) char* strndup(const char* s, size_t n)
{ {
size_t len = strnlen(s, n); size_t len = strnlen(s, n);
char* p = malloc(len+1); char* p = MALLOC(len+1);
if (p) { if (p) {
memcpy(p, s, len); memcpy(p, s, len);
p[len] = 0; p[len] = 0;
@ -263,7 +289,7 @@ struct toml_table_t {
}; };
static inline void xfree(const void* x) { if (x) free((void*)x); } static inline void xfree(const void* x) { if (x) FREE((void*)x); }
enum tokentype_t { enum tokentype_t {
@ -390,7 +416,7 @@ static char* normalize_string(const char* src, int srclen,
/* scan forward on src */ /* scan forward on src */
for (;;) { for (;;) {
if (off >= max - 10) { /* have some slack for misc stuff */ if (off >= max - 10) { /* have some slack for misc stuff */
char* x = realloc(dst, max += 100); char* x = REALLOC(dst, max += 100);
if (!x) { if (!x) {
xfree(dst); xfree(dst);
snprintf(errbuf, errbufsz, "out of memory"); snprintf(errbuf, errbufsz, "out of memory");
@ -412,7 +438,7 @@ static char* normalize_string(const char* src, int srclen,
/* ch was backslash. we expect the escape char. */ /* ch was backslash. we expect the escape char. */
if (sp >= sq) { if (sp >= sq) {
snprintf(errbuf, errbufsz, "last backslash is invalid"); snprintf(errbuf, errbufsz, "last backslash is invalid");
free(dst); xfree(dst);
return 0; return 0;
} }
@ -436,7 +462,7 @@ static char* normalize_string(const char* src, int srclen,
for (int i = 0; i < nhex; i++) { for (int i = 0; i < nhex; i++) {
if (sp >= sq) { if (sp >= sq) {
snprintf(errbuf, errbufsz, "\\%c expects %d hex chars", ch, nhex); snprintf(errbuf, errbufsz, "\\%c expects %d hex chars", ch, nhex);
free(dst); xfree(dst);
return 0; return 0;
} }
ch = *sp++; ch = *sp++;
@ -445,7 +471,7 @@ static char* normalize_string(const char* src, int srclen,
: (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1); : (('A' <= ch && ch <= 'F') ? ch - 'A' + 10 : -1);
if (-1 == v) { if (-1 == v) {
snprintf(errbuf, errbufsz, "invalid hex chars for \\u or \\U"); snprintf(errbuf, errbufsz, "invalid hex chars for \\u or \\U");
free(dst); xfree(dst);
return 0; return 0;
} }
ucs = ucs * 16 + v; ucs = ucs * 16 + v;
@ -453,7 +479,7 @@ static char* normalize_string(const char* src, int srclen,
int n = toml_ucs_to_utf8(ucs, &dst[off]); int n = toml_ucs_to_utf8(ucs, &dst[off]);
if (-1 == n) { if (-1 == n) {
snprintf(errbuf, errbufsz, "illegal ucs code in \\u or \\U"); snprintf(errbuf, errbufsz, "illegal ucs code in \\u or \\U");
free(dst); xfree(dst);
return 0; return 0;
} }
off += n; off += n;
@ -469,7 +495,7 @@ static char* normalize_string(const char* src, int srclen,
case '\\': ch = '\\'; break; case '\\': ch = '\\'; break;
default: default:
snprintf(errbuf, errbufsz, "illegal escape char \\%c", ch); snprintf(errbuf, errbufsz, "illegal escape char \\%c", ch);
free(dst); xfree(dst);
return 0; return 0;
} }
@ -517,7 +543,7 @@ static char* normalize_key(context_t* ctx, token_t strtok)
/* newlines are not allowed in keys */ /* newlines are not allowed in keys */
if (strchr(ret, '\n')) { if (strchr(ret, '\n')) {
free(ret); xfree(ret);
e_bad_key_error(ctx, lineno); e_bad_key_error(ctx, lineno);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -594,7 +620,7 @@ static toml_keyval_t* create_keyval_in_table(context_t* ctx, toml_table_t* tab,
/* if key exists: error out. */ /* if key exists: error out. */
toml_keyval_t* dest = 0; toml_keyval_t* dest = 0;
if (check_key(tab, newkey, 0, 0, 0)) { if (check_key(tab, newkey, 0, 0, 0)) {
free(newkey); xfree(newkey);
e_key_exists_error(ctx, keytok); e_key_exists_error(ctx, keytok);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -602,15 +628,15 @@ static toml_keyval_t* create_keyval_in_table(context_t* ctx, toml_table_t* tab,
/* make a new entry */ /* make a new entry */
int n = tab->nkval; int n = tab->nkval;
toml_keyval_t** base; toml_keyval_t** base;
if (0 == (base = (toml_keyval_t**) realloc(tab->kval, (n+1) * sizeof(*base)))) { if (0 == (base = (toml_keyval_t**) REALLOC(tab->kval, (n+1) * sizeof(*base)))) {
free(newkey); xfree(newkey);
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
tab->kval = base; tab->kval = base;
if (0 == (base[n] = (toml_keyval_t*) calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = (toml_keyval_t*) CALLOC(1, sizeof(*base[n])))) {
free(newkey); xfree(newkey);
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -634,7 +660,7 @@ static toml_table_t* create_keytable_in_table(context_t* ctx, toml_table_t* tab,
/* if key exists: error out */ /* if key exists: error out */
toml_table_t* dest = 0; toml_table_t* dest = 0;
if (check_key(tab, newkey, 0, 0, &dest)) { if (check_key(tab, newkey, 0, 0, &dest)) {
free(newkey); /* don't need this anymore */ xfree(newkey); /* don't need this anymore */
/* special case: if table exists, but was created implicitly ... */ /* special case: if table exists, but was created implicitly ... */
if (dest && dest->implicit) { if (dest && dest->implicit) {
@ -649,15 +675,15 @@ static toml_table_t* create_keytable_in_table(context_t* ctx, toml_table_t* tab,
/* create a new table entry */ /* create a new table entry */
int n = tab->ntab; int n = tab->ntab;
toml_table_t** base; toml_table_t** base;
if (0 == (base = (toml_table_t**) realloc(tab->tab, (n+1) * sizeof(*base)))) { if (0 == (base = (toml_table_t**) REALLOC(tab->tab, (n+1) * sizeof(*base)))) {
free(newkey); xfree(newkey);
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
tab->tab = base; tab->tab = base;
if (0 == (base[n] = (toml_table_t*) calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) {
free(newkey); xfree(newkey);
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -684,7 +710,7 @@ static toml_array_t* create_keyarray_in_table(context_t* ctx,
/* if key exists: error out */ /* if key exists: error out */
toml_array_t* dest = 0; toml_array_t* dest = 0;
if (check_key(tab, newkey, 0, &dest, 0)) { if (check_key(tab, newkey, 0, &dest, 0)) {
free(newkey); /* don't need this anymore */ xfree(newkey); /* don't need this anymore */
/* special case skip if exists? */ /* special case skip if exists? */
if (skip_if_exist) return dest; if (skip_if_exist) return dest;
@ -696,15 +722,15 @@ static toml_array_t* create_keyarray_in_table(context_t* ctx,
/* make a new array entry */ /* make a new array entry */
int n = tab->narr; int n = tab->narr;
toml_array_t** base; toml_array_t** base;
if (0 == (base = (toml_array_t**) realloc(tab->arr, (n+1) * sizeof(*base)))) { if (0 == (base = (toml_array_t**) REALLOC(tab->arr, (n+1) * sizeof(*base)))) {
free(newkey); xfree(newkey);
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
tab->arr = base; tab->arr = base;
if (0 == (base[n] = (toml_array_t*) calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = (toml_array_t*) CALLOC(1, sizeof(*base[n])))) {
free(newkey); xfree(newkey);
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -722,13 +748,13 @@ static toml_array_t* create_array_in_array(context_t* ctx,
{ {
int n = parent->nelem; int n = parent->nelem;
toml_array_t** base; toml_array_t** base;
if (0 == (base = (toml_array_t**) realloc(parent->u.arr, (n+1) * sizeof(*base)))) { if (0 == (base = (toml_array_t**) REALLOC(parent->u.arr, (n+1) * sizeof(*base)))) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
parent->u.arr = base; parent->u.arr = base;
if (0 == (base[n] = (toml_array_t*) calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = (toml_array_t*) CALLOC(1, sizeof(*base[n])))) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -743,13 +769,13 @@ static toml_table_t* create_table_in_array(context_t* ctx,
{ {
int n = parent->nelem; int n = parent->nelem;
toml_table_t** base; toml_table_t** base;
if (0 == (base = (toml_table_t**) realloc(parent->u.tab, (n+1) * sizeof(*base)))) { if (0 == (base = (toml_table_t**) REALLOC(parent->u.tab, (n+1) * sizeof(*base)))) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
parent->u.tab = base; parent->u.tab = base;
if (0 == (base[n] = (toml_table_t*) calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return 0; /* not reached */ return 0; /* not reached */
} }
@ -852,7 +878,7 @@ static void parse_array(context_t* ctx, toml_array_t* arr)
} }
/* make a new value in array */ /* make a new value in array */
char** tmp = (char**) realloc(arr->u.val, (arr->nelem+1) * sizeof(*tmp)); char** tmp = (char**) REALLOC(arr->u.val, (arr->nelem+1) * sizeof(*tmp));
if (!tmp) { if (!tmp) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return; /* not reached */ return; /* not reached */
@ -1083,14 +1109,14 @@ static void walk_tabpath(context_t* ctx)
default: default:
{ /* Not found. Let's create an implicit table. */ { /* Not found. Let's create an implicit table. */
int n = curtab->ntab; int n = curtab->ntab;
toml_table_t** base = (toml_table_t**) realloc(curtab->tab, (n+1) * sizeof(*base)); toml_table_t** base = (toml_table_t**) REALLOC(curtab->tab, (n+1) * sizeof(*base));
if (0 == base) { if (0 == base) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return; /* not reached */ return; /* not reached */
} }
curtab->tab = base; curtab->tab = base;
if (0 == (base[n] = (toml_table_t*) calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = (toml_table_t*) CALLOC(1, sizeof(*base[n])))) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return; /* not reached */ return; /* not reached */
} }
@ -1139,7 +1165,7 @@ static void parse_select(context_t* ctx)
/* For [x.y.z] or [[x.y.z]], remove z from tpath. /* For [x.y.z] or [[x.y.z]], remove z from tpath.
*/ */
token_t z = ctx->tpath.tok[ctx->tpath.top-1]; token_t z = ctx->tpath.tok[ctx->tpath.top-1];
free(ctx->tpath.key[ctx->tpath.top-1]); xfree(ctx->tpath.key[ctx->tpath.top-1]);
ctx->tpath.top--; ctx->tpath.top--;
/* set up ctx->curtab */ /* set up ctx->curtab */
@ -1166,14 +1192,14 @@ static void parse_select(context_t* ctx)
toml_table_t* dest; toml_table_t* dest;
{ {
int n = arr->nelem; int n = arr->nelem;
toml_table_t** base = realloc(arr->u.tab, (n+1) * sizeof(*base)); toml_table_t** base = REALLOC(arr->u.tab, (n+1) * sizeof(*base));
if (0 == base) { if (0 == base) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return; /* not reached */ return; /* not reached */
} }
arr->u.tab = base; arr->u.tab = base;
if (0 == (base[n] = calloc(1, sizeof(*base[n])))) { if (0 == (base[n] = CALLOC(1, sizeof(*base[n])))) {
e_outofmemory(ctx, FLINE); e_outofmemory(ctx, FLINE);
return; /* not reached */ return; /* not reached */
} }
@ -1235,7 +1261,7 @@ toml_table_t* toml_parse(char* conf,
ctx.tok.len = 0; ctx.tok.len = 0;
// make a root table // make a root table
if (0 == (ctx.root = calloc(1, sizeof(*ctx.root)))) { if (0 == (ctx.root = CALLOC(1, sizeof(*ctx.root)))) {
/* do not call outofmemory() here... setjmp not done yet */ /* do not call outofmemory() here... setjmp not done yet */
snprintf(ctx.errbuf, ctx.errbufsz, "ERROR: out of memory (%s)", FLINE); snprintf(ctx.errbuf, ctx.errbufsz, "ERROR: out of memory (%s)", FLINE);
return 0; return 0;
@ -1296,7 +1322,7 @@ toml_table_t* toml_parse_file(FILE* fp,
/* prime the buf[] */ /* prime the buf[] */
bufsz = 1000; bufsz = 1000;
if (! (buf = malloc(bufsz + 1))) { if (! (buf = MALLOC(bufsz + 1))) {
snprintf(errbuf, errbufsz, "out of memory"); snprintf(errbuf, errbufsz, "out of memory");
return 0; return 0;
} }
@ -1306,7 +1332,7 @@ toml_table_t* toml_parse_file(FILE* fp,
bufsz += 1000; bufsz += 1000;
/* Allocate 1 extra byte because we will tag on a NUL */ /* Allocate 1 extra byte because we will tag on a NUL */
char* x = realloc(buf, bufsz + 1); char* x = REALLOC(buf, bufsz + 1);
if (!x) { if (!x) {
snprintf(errbuf, errbufsz, "out of memory"); snprintf(errbuf, errbufsz, "out of memory");
xfree(buf); xfree(buf);
@ -1319,18 +1345,18 @@ toml_table_t* toml_parse_file(FILE* fp,
if (ferror(fp)) { if (ferror(fp)) {
snprintf(errbuf, errbufsz, "%s", snprintf(errbuf, errbufsz, "%s",
errno ? strerror(errno) : "Error reading file"); errno ? strerror(errno) : "Error reading file");
free(buf); xfree(buf);
return 0; return 0;
} }
off += n; off += n;
} }
/* tag on a NUL to cap the string */ /* tag on a NUL to cap the string */
buf[off] = 0; /* we accounted for this byte in the realloc() above. */ buf[off] = 0; /* we accounted for this byte in the REALLOC() above. */
/* parse it, cleanup and finish */ /* parse it, cleanup and finish */
toml_table_t* ret = toml_parse(buf, errbuf, errbufsz); toml_table_t* ret = toml_parse(buf, errbuf, errbufsz);
free(buf); xfree(buf);
return ret; return ret;
} }

41
toml.h
View file

@ -1,26 +1,26 @@
/* /*
MIT License MIT License
Copyright (c) 2017 - 2019 CK Tan Copyright (c) 2017 - 2019 CK Tan
https://github.com/cktan/tomlc99 https://github.com/cktan/tomlc99
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#ifndef TOML_H #ifndef TOML_H
#define TOML_H #define TOML_H
@ -137,6 +137,9 @@ TOML_EXTERN int toml_rtots(const char* s, toml_timestamp_t* ret);
/* misc */ /* misc */
TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret); TOML_EXTERN int toml_utf8_to_ucs(const char* orig, int len, int64_t* ret);
TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]); TOML_EXTERN int toml_ucs_to_utf8(int64_t code, char buf[6]);
TOML_EXTERN void toml_set_memutil(void* (*xxmalloc)(size_t),
void (*xxfree)(void*),
void* (*xxcalloc)(size_t, size_t),
void* (*xxrealloc)(void*, size_t));
#endif /* TOML_H */ #endif /* TOML_H */