Commit MetaInfo

Revision4e2cd63435d8ea9c352ab6a3edd82492399a1b5a (tree)
Time2020-04-21 20:33:22
AuthorHans Toshihide Törnqvist <hans.tornqvist@gmai...>
CommiterHans Toshihide Törnqvist

Log Message

Config fix reported by M. Munch and cppcheck pass.

Change Summary

Incremental Difference

diff -r fe04e75a07af -r 4e2cd63435d8 hgmake/common.c
--- a/hgmake/common.c Thu Oct 17 17:22:25 2019 +0200
+++ b/hgmake/common.c Tue Apr 21 13:33:22 2020 +0200
@@ -1,5 +1,5 @@
11 /*
2- * Copyright (c) 2016-2017, 2019
2+ * Copyright (c) 2016-2017, 2019-2020
33 * Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
44 *
55 * Permission to use, copy, modify, and/or distribute this software for any
@@ -71,9 +71,15 @@
7171 len = srclen + 1;
7272 dst = malloc(len);
7373 } else {
74+ void *dst_new;
75+
7476 dstlen = strlen(dst);
7577 len = dstlen + srclen + 1;
76- dst = realloc(dst, len);
78+ dst_new = realloc(dst, len);
79+ if (NULL == dst_new) {
80+ free(dst);
81+ }
82+ dst = dst_new;
7783 }
7884 if (NULL == dst) {
7985 err_(EXIT_FAILURE, "*alloc(%d)", len);
diff -r fe04e75a07af -r 4e2cd63435d8 htest/include/htest/htest.h
--- a/htest/include/htest/htest.h Thu Oct 17 17:22:25 2019 +0200
+++ b/htest/include/htest/htest.h Tue Apr 21 13:33:22 2020 +0200
@@ -1,5 +1,5 @@
11 /*
2- * Copyright (c) 2014-2019 Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
2+ * Copyright (c) 2014-2020 Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
33 *
44 * Permission to use, copy, modify, and/or distribute this software for any
55 * purpose with or without fee is hereby granted, provided that the above
@@ -157,11 +157,11 @@
157157 char const *bb_ = b;\
158158 if (NULL == aa_) {\
159159 HTRY_FAIL_HEADER_;\
160- printf("'%s'=\"%s\" "#op" '%s'=\"%s\".\n", #a, aa_, #b, bb_);\
160+ printf("'%s'=NULL "#op" '%s'=\"%s\".\n", #a, #b, bb_);\
161161 HTRY_FAIL_FOOTER_;\
162162 } else if (NULL == bb_) {\
163163 HTRY_FAIL_HEADER_;\
164- printf("'%s'=\"%s\" "#op" '%s'=\"%s\".\n", #a, aa_, #b, bb_);\
164+ printf("'%s'=\"%s\" "#op" '%s'=NULL.\n", #a, aa_, #b);\
165165 HTRY_FAIL_FOOTER_;\
166166 } else if ((size_t)-1 == siz) {\
167167 if (!(strcmp(aa_, bb_) op 0)) {\
diff -r fe04e75a07af -r 4e2cd63435d8 hutils/src/config.c
--- a/hutils/src/config.c Thu Oct 17 17:22:25 2019 +0200
+++ b/hutils/src/config.c Tue Apr 21 13:33:22 2020 +0200
@@ -1,5 +1,6 @@
11 /*
2- * Copyright (c) 2015-2018 Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
2+ * Copyright (c) 2015-2018, 2020
3+ * Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
34 *
45 * Permission to use, copy, modify, and/or distribute this software for any
56 * purpose with or without fee is hereby granted, provided that the above
@@ -23,9 +24,15 @@
2324 #include <hutils/string.h>
2425 #include <hutils/time.h>
2526
27+enum ConfigType {
28+ CONFIG_TYPE_D,
29+ CONFIG_TYPE_I32,
30+ CONFIG_TYPE_STR
31+};
2632 TAILQ_HEAD(ConfigList, Config);
2733 struct Config {
2834 char *name;
35+ enum ConfigType type;
2936 double d;
3037 int32_t i32;
3138 char *str;
@@ -153,12 +160,19 @@
153160
154161 fprintf(file, "[%s]\n", section->name);
155162 TAILQ_FOREACH(config, &section->config_list, next) {
156- if (config->str) {
163+ switch (config->type) {
164+ case CONFIG_TYPE_D:
165+ fprintf(file, "%s=%f\n", config->name,
166+ config->d);
167+ break;
168+ case CONFIG_TYPE_I32:
169+ fprintf(file, "%s=%d\n", config->name,
170+ config->i32);
171+ break;
172+ case CONFIG_TYPE_STR:
157173 fprintf(file, "%s=\"%s\"\n", config->name,
158174 config->str);
159- } else {
160- fprintf(file, "%s=%s\n", config->name,
161- config->str);
175+ break;
162176 }
163177 }
164178 }
@@ -232,6 +246,7 @@
232246 int len, ret;
233247
234248 FREE(a_config->str);
249+ a_config->type = CONFIG_TYPE_D;
235250 a_config->d = a_d;
236251 a_config->i32 = (int32_t)a_d;
237252 len = 24;
@@ -247,6 +262,7 @@
247262 int len, ret;
248263
249264 FREE(a_config->str);
265+ a_config->type = CONFIG_TYPE_I32;
250266 a_config->d = a_i32;
251267 a_config->i32 = a_i32;
252268 len = 16;
@@ -260,6 +276,7 @@
260276 config_sets(struct Config *a_config, char const *a_str)
261277 {
262278 FREE(a_config->str);
279+ a_config->type = CONFIG_TYPE_STR;
263280 if ('0' == a_str[0] && 'x' == a_str[1]) {
264281 a_config->i32 = strtol(a_str + 2, NULL, 16);
265282 a_config->d = a_config->i32;
@@ -394,6 +411,9 @@
394411 FREE(name);
395412 config_sets(config, token.str);
396413 FREE(token.str);
414+ if (LEXER_NUMBER == token.type) {
415+ config->type = CONFIG_TYPE_D;
416+ }
397417 } else {
398418 LOAD_ERROR("Missing section or config name");
399419 }
diff -r fe04e75a07af -r 4e2cd63435d8 hutils/src/file.c
--- a/hutils/src/file.c Thu Oct 17 17:22:25 2019 +0200
+++ b/hutils/src/file.c Tue Apr 21 13:33:22 2020 +0200
@@ -1,5 +1,6 @@
11 /*
2- * Copyright (c) 2017 Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
2+ * Copyright (c) 2017, 2020
3+ * Hans Toshihide Törnqvist <hans.tornqvist@gmail.com>
34 *
45 * Permission to use, copy, modify, and/or distribute this software for any
56 * purpose with or without fee is hereby granted, provided that the above
@@ -25,8 +26,8 @@
2526 file_load(char const *a_path, size_t a_max_size)
2627 {
2728 struct stat st;
28- FILE *file;
29- char *buf;
29+ FILE *file = NULL;
30+ char *buf = NULL;
3031
3132 if (0 != stat(a_path, &st)) {
3233 hutils_warn("stat(%s)", a_path);
@@ -45,14 +46,17 @@
4546 buf = malloc(st.st_size + 1);
4647 if (NULL == buf) {
4748 hutils_warn("malloc(%lu)", (unsigned long)st.st_size);
48- return NULL;
49+ goto file_load_fail;
4950 }
5051 if (fread(buf, 1, st.st_size, file) != (size_t)st.st_size) {
5152 hutils_warn("fread(%s)", a_path);
52- fclose(file);
53- return NULL;
53+ goto file_load_fail;
5454 }
5555 fclose(file);
5656 buf[st.st_size] = '\0';
5757 return buf;
58+file_load_fail:
59+ free(buf);
60+ fclose(file);
61+ return NULL;
5862 }
Show on old repository browser