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 22bc32d4589066520a22183a66d019db7f8f75f3
parent 100a169767e29adb5b1e29b3e494d79f9dddd3cd
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Wed,  5 Mar 2025 13:20:27 +0100

Wrap ref puts in NULL check and fix typos

Ensure ref_put is only called on allocated memory by verifying the
presence of optional volume and surface members. This prevents potential
issues with uninitialized pointers.

Diffstat:
Msrc/sphin_brdf.c | 2+-
Msrc/sphin_geometry.c | 12+++++++-----
Msrc/sphin_surface.c | 11++++++++---
Msrc/sphin_volume.c | 6++++--
4 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/src/sphin_brdf.c b/src/sphin_brdf.c @@ -120,7 +120,7 @@ parse_brdf else { res = RES_BAD_ARG; goto error; } /* Parse reflectivity value */ - str_reflectivity = strtok_r(value, " \t", &token_ptr); + str_reflectivity = strtok_r(NULL, " \t", &token_ptr); res = cstr_to_double(str_reflectivity, &reflectivity); if (RES_OK != res) { goto error; } if (reflectivity < 0 || reflectivity >1 ) { res = RES_BAD_ARG; goto error; } diff --git a/src/sphin_geometry.c b/src/sphin_geometry.c @@ -132,9 +132,6 @@ geometry_parse res = geometry_create(sphin, filename, &geom); if (RES_OK != res) { goto error; } - coords = darray_double_data_get(&geom->coords); - indices = darray_size_t_data_get(&geom->indices); /* Parse side */ - if (0 == strcmp(side, "FRONT")){ geom->side = SPHIN_SIDE_FRONT; } else if (0 == strcmp(side, "BACK")){ geom->side = SPHIN_SIDE_BACK; } else { res = RES_BAD_ARG; goto error; } @@ -144,12 +141,17 @@ geometry_parse if (RES_OK != res) { goto error; } res = sstl_get_desc(sstl, &sstl_desc); if (RES_OK != res) { goto error; } + + /* Parse coordinates and indices */ 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) { + coords = darray_double_data_get(&geom->coords); + indices = darray_size_t_data_get(&geom->indices); + + FOR_EACH(i, 0, sstl_desc.vertices_count*3) { coords[i] = (double)sstl_desc.vertices[i]; } - FOR_EACH(i, 0, sstl_desc.triangles_count) { + FOR_EACH(i, 0, sstl_desc.triangles_count*3) { indices[i] = (size_t)sstl_desc.indices[i]; } diff --git a/src/sphin_surface.c b/src/sphin_surface.c @@ -77,6 +77,7 @@ surface_create surface->source = NULL; str_init(sphin->allocator, &surface->name); + darray_sphin_geometry_ptr_init(sphin->allocator, &surface->geometries); res = str_set(&surface->name, name); if (RES_OK != res) { goto error; } @@ -115,7 +116,10 @@ release_surface(ref_T* address) for (i=ngeometries; i; i--) { SPHIN(geometry_ref_put(geometries[i-1])); }; - darray_sphin_geometry_ptr_release(&surface->geometries); + + if (NULL != geometries) { + darray_sphin_geometry_ptr_release(&surface->geometries); + } sphin = surface->sphin; brdf = surface->brdf; @@ -128,7 +132,9 @@ release_surface(ref_T* address) if (NULL != sensor) { SPHIN(sensor_ref_put(sensor)); } - SPHIN(brdf_ref_put(brdf)); + if (NULL != brdf) { + SPHIN(brdf_ref_put(brdf)); + } SPHIN(ref_put(sphin)); } @@ -198,7 +204,6 @@ parse_surface if (NULL == token){ res = RES_BAD_ARG; goto error; } 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; } diff --git a/src/sphin_volume.c b/src/sphin_volume.c @@ -108,8 +108,10 @@ release_volume(ref_T* address) /* Put references for each one of the geometries */ for (i=ngeometries; i; i--) { SPHIN(geometry_ref_put(geometries[i-1])); - }; - darray_sphin_geometry_ptr_release(&volume->geometries); + } + if (NULL != geometries) { + darray_sphin_geometry_ptr_release(&volume->geometries); + } sphin = volume->sphin; sensor = volume->sensor;