From f76d9811f7722545fd8762e712d0cd8f84066c7d Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 24 Feb 2016 18:06:49 +0100 Subject: [PATCH] sdb_fmt_tobin returns the number of items and fixes a double-alloc --- shlr/sdb/src/fmt.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/shlr/sdb/src/fmt.c b/shlr/sdb/src/fmt.c index 8149247a4d..f6ac0c75b7 100644 --- a/shlr/sdb/src/fmt.c +++ b/shlr/sdb/src/fmt.c @@ -82,12 +82,12 @@ SDB_API char *sdb_fmt_tostr(void *p, const char *fmt) { // TODO: return false if array length != fmt length SDB_API int sdb_fmt_tobin(const char *_str, const char *fmt, void *stru) { - int n, idx = 0; + int n, items, idx = 0; char *next, *str, *ptr, *word, *e_str; if (!_str || !*_str || !fmt) return 0; str = ptr = strdup (_str); - for (; *fmt; fmt++) { + for (items = 0; *fmt; fmt++, items++) { word = sdb_anext (ptr, &next); if (!word || !*word) break; @@ -99,8 +99,7 @@ SDB_API int sdb_fmt_tobin(const char *_str, const char *fmt, void *stru) { case 'h': *((short*)(stru + idx)) = (short)sdb_atoi (word); break; case 's': e_str = (char*)sdb_decode (word, 0); - *((char**)(stru + idx)) = (char*)strdup (e_str?e_str:word); - free (e_str); + *((char**)(stru + idx)) = e_str? e_str: strdup (word); break; case 'z': *((char**)(stru + idx)) = (char*)strdup (word); @@ -108,13 +107,13 @@ SDB_API int sdb_fmt_tobin(const char *_str, const char *fmt, void *stru) { case 'p': *((void**)(stru + idx)) = (void*)(size_t)sdb_atoi (word); break; } - idx += R_MAX((long)sizeof (void*), n); // align + idx += R_MAX ((long) sizeof (void*), n); // align if (!next) break; ptr = next; } free (str); - return 1; + return items; } SDB_API void sdb_fmt_free (void *stru, const char *fmt) {