rnatm

Load and structure data describing an atmosphere
git clone git://git.meso-star.fr/rnatm.git
Log | Files | Refs | README | LICENSE

commit e8c8103518187338164104fe5712a99e948a1b33
parent 004fa7d27a5892e18bf3e2d17430929535ad2365
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 22 Aug 2022 15:03:54 +0200

Fix parsing of test_rnatm program arguments

Parsing a bogus -g option could lead to a segfault

Diffstat:
Msrc/test_rnatm.c | 47+++++++++++++++++++++++++++++++++--------------
1 file changed, 33 insertions(+), 14 deletions(-)

diff --git a/src/test_rnatm.c b/src/test_rnatm.c @@ -87,6 +87,7 @@ print_help(const char* cmd) static res_T parse_gas_parameters(const char* str, void* ptr) { + enum { MESH, CK, TEMP } iparam; char buf[BUFSIZ]; wordexp_t wexp; struct args* args = ptr; @@ -108,6 +109,24 @@ parse_gas_parameters(const char* str, void* ptr) key = strtok_r(buf, "=", &tk_ctx); val = strtok_r(NULL, "", &tk_ctx); + if(!strcmp(key, "mesh")) { + iparam = MESH; + } else if(!strcmp(key, "ck")) { + iparam = CK; + } else if(!strcmp(key, "temp")) { + iparam = TEMP; + } else { + fprintf(stderr, "Invalid gas parameter `%s'\n", key); + res = RES_BAD_ARG; + goto error; + } + + if(!val) { + fprintf(stderr, "Invalid null value for gas parameter `%s'\n", key); + res = RES_BAD_ARG; + goto error; + } + err = wordexp(val, &wexp, 0/*flags*/); if(err) { fprintf(stderr, "Unable to expand the parameter value `%s'\n", str); @@ -121,21 +140,21 @@ parse_gas_parameters(const char* str, void* ptr) goto error; } - if(!strcmp(key, "mesh")) { - args->rnatm.gas.smsh_filename = strdup(val); - if(!args->rnatm.gas.smsh_filename) res = RES_MEM_ERR; - } else if(!strcmp(key, "ck")) { - args->rnatm.gas.sck_filename = strdup(val); - if(!args->rnatm.gas.sck_filename) res = RES_MEM_ERR; - } else if(!strcmp(key, "temp")) { - args->rnatm.gas.temperatures_filename = strdup(val); - if(!args->rnatm.gas.temperatures_filename) res = RES_MEM_ERR; - } else { - fprintf(stderr, "Invalid gas parameter `%s'\n", key); - res = RES_BAD_ARG; - goto error; + switch(iparam) { + case MESH: + args->rnatm.gas.smsh_filename = strdup(val); + if(!args->rnatm.gas.smsh_filename) res = RES_MEM_ERR; + break; + case CK: + args->rnatm.gas.sck_filename = strdup(val); + if(!args->rnatm.gas.sck_filename) res = RES_MEM_ERR; + break; + case TEMP: + args->rnatm.gas.temperatures_filename = strdup(val); + if(!args->rnatm.gas.temperatures_filename) res = RES_MEM_ERR; + break; + default: FATAL("Unreachable code\n"); break; } - if(res != RES_OK) { fprintf(stderr, "Unable to parse the gas parameter `%s' -- %s\n", str, res_to_cstr(res));