star-phor-input

File format for describing photoreactor configurations
git clone https://www.edstar.cnrs.fr/git/star-phor-input.git
Log | Files | Refs | README | LICENSE

commit 1c228a6c25733d273b3e70f1b1c104dc764889b1
parent 53b20438bcb538904857945febd60be357ea0626
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Wed, 19 Mar 2025 10:50:44 +0100

Forbid names for sensor and sources in the input file

Disallowing sensor and source names in the input file may result in
token being NULL during surface and volume processing. To handle this,
non-null token verification has been moved to deeper parsing functions
(e.g., ka, geometry, response_function, etc.), ensuring that elements
requiring a value after a keyword enforce this check.

Additionally, further ASSERTs have been added to parsing functions,
abstracting those previously performed by callers.

Diffstat:
Msrc/sphin_brdf.c | 4++++
Msrc/sphin_config.c | 2+-
Msrc/sphin_geometry.c | 5+++++
Msrc/sphin_sensor.c | 8+++++++-
Msrc/sphin_source.c | 6++++++
Msrc/sphin_surface.c | 16++++++++++++++--
Msrc/sphin_volume.c | 16+++++++++++++---
Msrc/test_sphin_load_source.c | 22+++++++++++++++++++---
Msrc/test_sphin_load_volume.c | 22+++++++++++++++++++++-
9 files changed, 90 insertions(+), 11 deletions(-)

diff --git a/src/sphin_brdf.c b/src/sphin_brdf.c @@ -105,6 +105,10 @@ parse_brdf res_T res = RES_OK; ASSERT(NULL != sphin); + ASSERT(NULL != txtrdr); + ASSERT(NULL != out_brdf); + + if (NULL == value) { res = RES_BAD_ARG; goto error; } res = brdf_create(sphin, &brdf); if (RES_OK != res) { goto error; } diff --git a/src/sphin_config.c b/src/sphin_config.c @@ -160,7 +160,7 @@ load_stream keyword = trim_keyword(token); if (NULL == keyword){ res = RES_BAD_ARG; goto error; } /* parse value (name) */ - token = strtok_r(NULL, "", &token_ptr); + token = token_ptr; if (NULL == token){ res = RES_BAD_ARG; goto error; } name = trim_string(token); if (NULL == name){ res = RES_BAD_ARG; goto error; } diff --git a/src/sphin_geometry.c b/src/sphin_geometry.c @@ -111,6 +111,11 @@ geometry_parse size_t* indices = NULL; res_T res = RES_OK; + ASSERT(NULL != sphin); + ASSERT(NULL != out_geom); + + if (NULL == value) { res = RES_BAD_ARG; goto error; } + /* Create the SSTL device using the same allocator and logger as the sphin * handler. We intentionally instantiate and free the SSTL struct for * each geometry, rather than creating it once at the sphin_config level. diff --git a/src/sphin_sensor.c b/src/sphin_sensor.c @@ -106,6 +106,9 @@ parse_response_function double response_function; ASSERT(NULL != sensor); + ASSERT(NULL != txtrdr); + + if (NULL == value) { res = RES_BAD_ARG; goto error; } token = strtok_r(value, " \t", &token_ptr); @@ -145,6 +148,9 @@ parse_sensor res_T res = RES_OK; ASSERT(NULL != sphin); + ASSERT(NULL != txtrdr); + ASSERT(NULL != name); + ASSERT(NULL != out_sensor); str_init(sphin->allocator, &line); @@ -166,7 +172,6 @@ parse_sensor /* Parse value */ token = strtok_r(NULL, "", &token_ptr); - if (NULL == token){ res = RES_BAD_ARG; goto error; } value = token; /* Parse response function */ if (0 == strcmp(keyword, "response_function")){ @@ -177,6 +182,7 @@ parse_sensor } if (RES_OK != res) { goto error; } } + exit: str_release(&line); *out_sensor = sensor; diff --git a/src/sphin_source.c b/src/sphin_source.c @@ -114,6 +114,9 @@ parse_direction_distribution res_T res = RES_OK; ASSERT(NULL != source); + ASSERT(NULL != txtrdr); + + if (NULL == value) { res = RES_BAD_ARG; goto error; }; /* Parse direction distribution type */ direction_distribution_type = strtok_r(value, " \t", &token_ptr); @@ -172,6 +175,9 @@ parse_flux_density res_T res = RES_OK; ASSERT(NULL != source); + ASSERT(NULL != txtrdr); + + if (NULL == value) { res = RES_BAD_ARG; goto error; } /* Parse flux density value */ str_flux_density = strtok_r(value, " \t", &token_ptr); diff --git a/src/sphin_surface.c b/src/sphin_surface.c @@ -148,6 +148,9 @@ parse_geometry res_T res = RES_OK; ASSERT(NULL != surface); + ASSERT(NULL != txtrdr); + + if(NULL == value) {res = RES_BAD_ARG; goto error; } res = geometry_parse(surface->sphin, value, &geom); if (RES_OK != res) { goto error; } @@ -204,8 +207,7 @@ parse_surface keyword = trim_keyword(token); if (NULL == keyword){ res = RES_BAD_ARG; goto error; } /* parse value */ - token = strtok_r(NULL, "", &token_ptr); - if (NULL == token){ res = RES_BAD_ARG; goto error; } + token = token_ptr; if (0 == strcmp(keyword, "geometry")){ res = parse_geometry(surface, txtrdr, token); } @@ -213,9 +215,19 @@ parse_surface res = parse_brdf(surface->sphin, txtrdr, token, &surface->brdf); } else if (0 == strcmp(keyword, "source")) { + /* source should not have a name. + * strings are not allowed in the input file after the : + * check if there is only tabs and spaces after : */ + token = strtok_r(NULL, " \t", &token_ptr); + if (NULL != token) { res = RES_BAD_ARG; goto error; } res = parse_source(surface->sphin, txtrdr, name, &surface->source); } else if (0 == strcmp(keyword, "sensor")) { + /* sensor should not have a name. + * strings are not allowed in the input file after the : + * check if there is only tabs and spaces after : */ + token = strtok_r(NULL, " \t", &token_ptr); + if (NULL != token) { res = RES_BAD_ARG; goto error; } res = parse_sensor(surface->sphin, txtrdr, name, &surface->sensor); } else { diff --git a/src/sphin_volume.c b/src/sphin_volume.c @@ -131,7 +131,10 @@ parse_geometry struct sphin_geometry* geom; res_T res = RES_OK; - ASSERT(NULL != volume); /* txtrdr and value are already asserted by caller */ + ASSERT(NULL != volume); + ASSERT(NULL != txtrdr); + + if (NULL == value){ res = RES_BAD_ARG; goto error; } res = geometry_parse(volume->sphin, value, &geom); if (RES_OK != res) { goto error; } @@ -163,6 +166,9 @@ parse_ka double ka; ASSERT(NULL != volume); + ASSERT(NULL != txtrdr); + + if (NULL == value){ res = RES_BAD_ARG; goto error; } /* Parse ka value */ ka_val = strtok_r(value, " \t", &token_ptr); @@ -239,8 +245,7 @@ parse_volume if (NULL == keyword){ res = RES_BAD_ARG; goto error; } /* parse value */ - token = strtok_r(NULL, "", &token_ptr); - if (NULL == token){ res = RES_BAD_ARG; goto error; } + token = token_ptr; if (0 == strcmp(keyword, "geometry")){ res = parse_geometry(volume, txtrdr, token); } @@ -248,6 +253,11 @@ parse_volume res = parse_ka(volume, txtrdr, token); } else if (0 == strcmp(keyword, "sensor")) { + /* for the sensor, + * strings are not allowed in the input file after the : + * check if there is only tabs and spaces after : */ + token = strtok_r(NULL, "\t ", &token_ptr); + if (NULL != token) { res = RES_BAD_ARG; goto error; } res = parse_sensor(volume->sphin, txtrdr, name, &volume->sensor); } else { diff --git a/src/test_sphin_load_source.c b/src/test_sphin_load_source.c @@ -1,6 +1,4 @@ - /* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique - * Copyright (C) 2024-2025 Clermont Auvergne INP * Copyright (C) 2024-2025 INSA Lyon * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux @@ -69,7 +67,7 @@ test_source_api CHK(fp = fopen(path, "w+")); fprintf(fp, "\t\t surface : \"surface name\"\n"); fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); - fprintf(fp, "\tsource:\t #commentaire\n"); + fprintf(fp, "\tsource:#commentaire\n"); fprintf(fp, "\tflux_density: 200e-6 mol/m^2/s\t\n"); fprintf(fp, "\tdirection: LAMBERT\n"); fprintf(fp, "\t\t surface : \"surface name 2\"\n"); @@ -217,6 +215,23 @@ test_source_api_bad_collimation_degree CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); } +static void +test_source_api_bad_name + (struct sphin* sphin) +{ + const char* path = "file.txt"; + struct sphin_config* config = NULL; + + FILE* fp = NULL; + + CHK(fp = fopen(path, "w+")); + fprintf(fp, "\t\t surface : \"surface name\"\n"); + fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); + fprintf(fp, "\tsource:\t \"blabla\" #commentaire\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); +} int main(int argc, char** argv) { @@ -237,6 +252,7 @@ main(int argc, char** argv) test_source_api_bad_flux_density_value(sphin); test_source_api_bad_collimation_degree(sphin); test_source_api_bad_direction(sphin); + test_source_api_bad_name(sphin); CHK(sphin_ref_put(sphin) == RES_OK); diff --git a/src/test_sphin_load_volume.c b/src/test_sphin_load_volume.c @@ -101,7 +101,7 @@ test_load_volume fprintf(fp, "#Mot Clé Nom\n"); fprintf(fp, "volume: \"reaction volume\"\n"); fprintf(fp, "\tka: 1 m^-1\n"); - fprintf(fp, "\tsensor: \"my sensor\"\n"); + fprintf(fp, "\tsensor:\n"); fprintf(fp, "\t\tresponse_function: 1\n"); fprintf(fp, "\tgeometry: FRONT test_0.stl\n"); fprintf(fp, "\tgeometry: BACK test_3.stl\n"); @@ -185,6 +185,25 @@ test_load_volume_bad_geometry } static void +test_load_volume_bad_sensor + (struct sphin* sphin) +{ + struct sphin_config* config = NULL; + const char* path = "file.txt"; + FILE* fp = NULL; + + CHK(fp = fopen(path, "w+")); + fprintf(fp, "#Mot Clé Nom\n"); + fprintf(fp, " volume\t: \"reaction volume\"\n"); + fprintf(fp, "\tka: 1 cm^-1\n"); + fprintf(fp, "\tgeometry: BACK test_2.stl\n"); + fprintf(fp, "\tsensor: \"blabla\"\n"); /* Sensor cannot have a name */ + fprintf(fp, "\t\tresponse_function: 1\n"); + fclose(fp); + + CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); +} +static void test_load_empty_file (struct sphin* sphin) { @@ -217,6 +236,7 @@ main(int argc, char** argv) test_load_empty_file(sphin); test_load_volume(sphin); test_load_volume_bad_geometry(sphin); + test_load_volume_bad_sensor(sphin); CHK(sphin_ref_put(sphin) == RES_OK);