commit 042da9c7f7790d50d94234fdbe7788bad8d0895d
parent 30c44e864f356da6f2dca17e81710fcda7ff6975
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Mon, 3 Mar 2025 10:01:43 +0100
Fix infinite loop in volume, surface, sensor, and source parsing
The function responsible for incrementing the line number was not being
called during the parsing of theses structs, causing the parser to get
stuck on the first line of these entities in the input file. Ensure
proper line reads to avoid an infinite loop.
Make minor code style adjustments.
Diffstat:
7 files changed, 56 insertions(+), 35 deletions(-)
diff --git a/src/sphin_brdf.c b/src/sphin_brdf.c
@@ -107,7 +107,7 @@ parse_brdf
ASSERT(NULL != sphin);
(void) txtrdr; /* Disable unused variable warning */
res = brdf_create(sphin, &brdf);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
/* Parse brdf type */
brdf_type = strtok_r(value, " \t", &token_ptr);
if (NULL == brdf_type){ res = RES_BAD_ARG; goto error; }
@@ -122,7 +122,7 @@ parse_brdf
/* Parse reflectivity value */
str_reflectivity = strtok_r(value, " \t", &token_ptr);
res = cstr_to_double(str_reflectivity, &reflectivity);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
if (reflectivity < 0 || reflectivity >1 ) { res = RES_BAD_ARG; goto error; }
switch(brdf->type) {
diff --git a/src/sphin_config.c b/src/sphin_config.c
@@ -56,9 +56,12 @@ config_create
res = RES_MEM_ERR;
goto error;
}
+
ref_init(&config->ref);
- SPHIN(ref_get(sphin));
+ darray_sphin_volume_ptr_init(sphin->allocator, &config->volumes);
+ darray_sphin_surface_ptr_init(sphin->allocator, &config->surfaces);
config->sphin = sphin;
+ SPHIN(ref_get(sphin));
exit:
*out_config = config;
@@ -79,7 +82,6 @@ release_config(ref_T* address)
size_t i, nvolumes, nsurfaces;
struct sphin_volume** volumes;
struct sphin_surface** surfaces;
-
ASSERT(NULL != address);
config = CONTAINER_OF(address, struct sphin_config, ref);
@@ -87,7 +89,7 @@ release_config(ref_T* address)
* each one of the volumes */
nvolumes = darray_sphin_volume_ptr_size_get(&config->volumes);
volumes = darray_sphin_volume_ptr_data_get(&config->volumes);
- ASSERT(NULL != volumes);
+
/* Put references for each one of the volumes */
for (i=nvolumes; i; i--) {
SPHIN(volume_ref_put(volumes[i-1]));
@@ -98,7 +100,7 @@ release_config(ref_T* address)
* each one of the surfaces */
nsurfaces = darray_sphin_surface_ptr_size_get(&config->surfaces);
surfaces = darray_sphin_surface_ptr_data_get(&config->surfaces);
- ASSERT(NULL != surfaces);
+
/* Put references for each one of the surfaces */
for (i=nsurfaces; i; i--) {
SPHIN(surface_ref_put(surfaces[i-1]));
@@ -145,12 +147,10 @@ load_stream
if (RES_OK != res) {
goto error;
}
-
res = txtrdr_read_line(txtrdr);
if (RES_OK != res) {
goto error;
}
-
while (NULL != txtrdr_get_line(txtrdr)) {
res = str_set(&line, txtrdr_get_cline(txtrdr));
if (RES_OK != res) { goto error; }
@@ -162,10 +162,9 @@ load_stream
/* parse value (name) */
token = strtok_r(NULL, "", &token_ptr);
- if (NULL != token){ res = RES_BAD_ARG; goto error; }
+ if (NULL == token){ res = RES_BAD_ARG; goto error; }
name = trim_string(token);
if (NULL == name){ res = RES_BAD_ARG; goto error; }
-
if (0 == strcmp(keyword, "volume")){
res = parse_volume(config, txtrdr, name);
}
@@ -187,6 +186,10 @@ load_stream
txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr));
goto error;
}
+ res = txtrdr_read_line(txtrdr);
+ if (RES_OK != res) {
+ goto error;
+ }
}
exit:
diff --git a/src/sphin_geometry.c b/src/sphin_geometry.c
@@ -110,7 +110,7 @@ geometry_parse
/* Create the sstl device with the same allocator and logger as the sphin
* handler. TODO Comment*/
res = sstl_create(sphin->logger, sphin->allocator, 1, &sstl);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
/* Parse filename */
filename = strtok_r(NULL, " \t", &token_ptr);
@@ -118,7 +118,7 @@ geometry_parse
/* Create geometry */
res = geometry_create(sphin, filename, &geom);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
coords = darray_double_data_get(&geom->coords);
indices = darray_size_t_data_get(&geom->indices); /* Parse side */
@@ -130,9 +130,9 @@ geometry_parse
/* Load the stl */
res = sstl_load(sstl, filename);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
res = sstl_get_desc(sstl, &sstl_desc);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
darray_double_resize(&geom->coords, sstl_desc.vertices_count*3);
darray_size_t_resize(&geom->indices, sstl_desc.triangles_count*3);
FOR_EACH(i, 0, sstl_desc.vertices_count) {
diff --git a/src/sphin_sensor.c b/src/sphin_sensor.c
@@ -112,7 +112,7 @@ parse_response_function
token = strtok_r(value, " \t", &token_ptr);
res = cstr_to_double(token, &response_function);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
if (response_function < 0) { res = RES_BAD_ARG; goto error; }
sensor->response_function = response_function;
@@ -148,7 +148,7 @@ parse_sensor
str_init(sphin->allocator, &line);
res = sensor_create(sphin, name, &sensor);
- if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
+ if (RES_OK != res) { goto error; }
while (NULL != txtrdr_get_line(txtrdr)) {
@@ -172,6 +172,9 @@ parse_sensor
else {
break;
}
+ /* Advance one line in the parsing */
+ res = txtrdr_read_line(txtrdr);
+ if (RES_OK != res) { goto error; }
}
exit:
diff --git a/src/sphin_source.c b/src/sphin_source.c
@@ -182,7 +182,7 @@ parse_flux_density
flux_density_unit = strtok_r(value, " \t", &token_ptr);
if (NULL == flux_density_unit){ res = RES_BAD_ARG; goto error; }
- /* Kinetic flux density unit */
+ /* Parse kinetic flux density unit */
if (0 == strcmp(flux_density_unit, "mol/m^2/s")
|| 0 == strcmp(flux_density_unit, "mol.m^-2.s^-1")) {
source->flux_density.unit= SPHIN_PHOTON_UNIT_MOL;
@@ -193,10 +193,15 @@ parse_flux_density
source->flux_density.unit= SPHIN_PHOTON_UNIT_MOL;
source->flux_density.flux_density *= 1; /* No conversion */
}
-
+
/* Energy flux density energy */
+ else if (0 == strcmp(flux_density_unit, "mW/m^2")
+ || 0 == strcmp(flux_density_unit, "mW.s^-1")) {
+ source->flux_density.unit= SPHIN_PHOTON_UNIT_JOULE;
+ source->flux_density.flux_density *= 1000;
+ }
else if (0 == strcmp(flux_density_unit, "W/m^2")
- || 0 == strcmp(flux_density_unit, "W.s^-1")) {
+ || 0 == strcmp(flux_density_unit, "W.s^-1")) {
source->flux_density.unit= SPHIN_PHOTON_UNIT_JOULE;
source->flux_density.flux_density *= 1; /* No conversion */
}
@@ -262,6 +267,9 @@ parse_source
else {
break;
}
+ /* Advance one line in the parsing */
+ res = txtrdr_read_line(txtrdr);
+ if (RES_OK != res) { goto error; }
}
exit:
diff --git a/src/sphin_surface.c b/src/sphin_surface.c
@@ -67,6 +67,8 @@ surface_create
surface = MEM_CALLOC(sphin->allocator, 1, sizeof(struct sphin_surface));
if (NULL == surface) { res = RES_MEM_ERR; goto error; }
+
+ /* Init volume ref counter and init member variables */
ref_init(&surface->ref);
SPHIN(ref_get(sphin));
surface->sphin = sphin;
@@ -101,6 +103,7 @@ release_surface(ref_T* address)
surface = CONTAINER_OF(address, struct sphin_surface, ref);
str_release(&surface->name);
+ darray_sphin_geometry_ptr_release(&surface->geometries);
sphin = surface->sphin;
brdf = surface->brdf;
sensor = surface->sensor;
@@ -157,7 +160,6 @@ parse_surface
char* keyword = NULL;
char* token = NULL;
char* token_ptr = NULL;
- char* value = NULL;
res_T res = RES_OK;
ASSERT(NULL != config);
@@ -186,29 +188,31 @@ parse_surface
/* parse value */
token = strtok_r(NULL, "", &token_ptr);
if (NULL == token){ res = RES_BAD_ARG; goto error; }
- value = token;
if (0 == strcmp(keyword, "geometry")){
- res = parse_geometry(surface, txtrdr, value);
+ res = parse_geometry(surface, txtrdr, token);
}
else if (0 == strcmp(keyword, "brdf")){
- res = parse_brdf(surface->sphin, txtrdr, value, &surface->brdf);
+ res = parse_brdf(surface->sphin, txtrdr, token, &surface->brdf);
}
else if (0 == strcmp(keyword, "source")) {
- value = trim_string(value);
+ token = trim_string(token);
// res = parse_source(surface->sphin, txtrdr, name, &surface->source);
}
else if (0 == strcmp(keyword, "sensor")) {
- value = trim_string(value);
+ token = trim_string(token);
res = parse_sensor(surface->sphin, txtrdr, name, &surface->sensor);
}
else {
break;
}
+ /* Advance one line in the parsing */
+ res = txtrdr_read_line(txtrdr);
+ if (RES_OK != res) { goto error; }
}
/* Append a new element to the end of the sphin_config->surfaces array */
res = darray_sphin_surface_ptr_push_back(&config->surfaces, &surface);
- if (RES_OK != res) {goto error; }
+ if (RES_OK != res) { goto error; }
exit:
str_release(&line);
diff --git a/src/sphin_volume.c b/src/sphin_volume.c
@@ -64,6 +64,8 @@ volume_create
volume = MEM_CALLOC(sphin->allocator, 1, sizeof(struct sphin_volume));
if (NULL == volume) { res = RES_MEM_ERR; goto error; }
+
+ /* Init volume ref counter and init member variables */
ref_init(&volume->ref);
SPHIN(ref_get(sphin));
volume->sphin = sphin;
@@ -88,9 +90,9 @@ error:
static void
release_volume(ref_T* address)
{
- struct sphin_volume* volume = NULL;
struct sphin* sphin = NULL;
struct sphin_sensor* sensor = NULL;
+ struct sphin_volume* volume = NULL;
ASSERT(NULL != address);
volume = CONTAINER_OF(address, struct sphin_volume, ref);
@@ -114,7 +116,7 @@ parse_geometry
struct sphin_geometry* geom;
res_T res = RES_OK;
- ASSERT(NULL != volume);
+ ASSERT(NULL != volume); /* txtrdr and value are already asserted by caller */
(void) txtrdr; /* Disable unused variable warning */
res = geometry_parse(volume->sphin, value, &geom);
@@ -193,7 +195,6 @@ parse_volume
char* keyword = NULL;
char* token = NULL;
char* token_ptr = NULL;
- char* value = NULL;
res_T res = RES_OK;
ASSERT(NULL != config);
@@ -222,25 +223,27 @@ parse_volume
/* parse value */
token = strtok_r(NULL, "", &token_ptr);
if (NULL == token){ res = RES_BAD_ARG; goto error; }
- value = token;
if (0 == strcmp(keyword, "geometry")){
- res = parse_geometry(volume, txtrdr, value);
+ res = parse_geometry(volume, txtrdr, token);
}
else if (0 == strcmp(keyword, "ka")){
- res = parse_ka(volume, txtrdr, value);
+ res = parse_ka(volume, txtrdr, token);
}
else if (0 == strcmp(keyword, "sensor")) {
- value = trim_string(value);
- res = parse_sensor(volume->sphin, txtrdr, name, &volume->sensor);
+ token = trim_string(token);
+ res = parse_sensor(volume->sphin, txtrdr, token, &volume->sensor);
}
else {
break;
}
+ /* Advance one line in the parsing */
+ res = txtrdr_read_line(txtrdr);
+ if (RES_OK != res) { goto error; }
}
/* Append a new element to the end of the sphin_config->volumes array */
res = darray_sphin_volume_ptr_push_back(&config->volumes, &volume);
- if (RES_OK != res) {goto error; }
+ if (RES_OK != res) { goto error; }
exit:
str_release(&line);