commit a895f87231647defdc62e0e07b3dd2f75e834c59
parent c8befaacbbb940e655d5b81ee85a58ed00321fc3
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Fri, 7 Mar 2025 18:10:52 +0100
Fix test-identified bugs, add API functions args verification
Resolved issues found during testing and introduced additional
checks to validate API function arguments.
Diffstat:
6 files changed, 36 insertions(+), 52 deletions(-)
diff --git a/src/sphin_brdf.c b/src/sphin_brdf.c
@@ -125,10 +125,6 @@ parse_brdf
if (RES_OK != res) { goto error; }
if (reflectivity < 0 || reflectivity >1 ) { res = RES_BAD_ARG; goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
-
switch(brdf->type) {
case SPHIN_BRDF_LAMBERT:
brdf->lambertian.reflectivity = reflectivity;
@@ -137,10 +133,10 @@ parse_brdf
brdf->specular.reflectivity = reflectivity;
break;
default: FATAL("Unreachable code\n"); break;
-
}
exit:
+ txtrdr_read_line(txtrdr);
*out_brdf = brdf;
return res;
error:
@@ -181,7 +177,7 @@ sphin_brdf_lambertian_get_reflectivity
{
res_T res = RES_OK;
- if (NULL == brdf) {
+ if (NULL == brdf || NULL == reflectivity) {
return RES_BAD_ARG;
}
if (SPHIN_BRDF_SPECULAR == brdf->type) {
@@ -198,12 +194,12 @@ sphin_brdf_specular_get_reflectivity
{
res_T res = RES_OK;
- if (NULL == brdf) {
+ if (NULL == brdf || NULL == reflectivity) {
return RES_BAD_ARG;
}
if (SPHIN_BRDF_LAMBERT == brdf->type) {
return RES_BAD_ARG;
}
- *reflectivity = brdf->lambertian.reflectivity;
+ *reflectivity = brdf->specular.reflectivity;
return res;
}
diff --git a/src/sphin_config.c b/src/sphin_config.c
@@ -155,7 +155,6 @@ load_stream
while (NULL != txtrdr_get_line(txtrdr)) {
res = str_set(&line, txtrdr_get_cline(txtrdr));
if (RES_OK != res) { goto error; }
-
/* parse keyword */
token = strtok_r(str_get(&line), ":", &token_ptr);
keyword = trim_keyword(token);
@@ -355,7 +354,7 @@ sphin_config_get_volume_count
size_t* nvolumes)
{
res_T res = RES_OK;
- if (NULL == config) {
+ if (NULL == config || NULL == nvolumes) {
return RES_BAD_ARG;
}
*nvolumes = darray_sphin_volume_ptr_size_get(&config->volumes);
@@ -371,7 +370,7 @@ sphin_config_get_volume
res_T res = RES_OK;
size_t nvolumes;
- if (NULL == config) {
+ if (NULL == config || NULL == volume) {
return RES_BAD_ARG;
}
@@ -390,7 +389,7 @@ sphin_config_get_surface_count
size_t* nsurfaces)
{
res_T res = RES_OK;
- if (NULL == config) {
+ if (NULL == config || NULL == nsurfaces) {
return RES_BAD_ARG;
}
*nsurfaces = darray_sphin_surface_ptr_size_get(&config->surfaces);
@@ -406,7 +405,7 @@ sphin_config_get_surface
res_T res = RES_OK;
size_t nsurfaces;
- if (NULL == config) {
+ if (NULL == config || NULL == surface) {
return RES_BAD_ARG;
}
diff --git a/src/sphin_sensor.c b/src/sphin_sensor.c
@@ -117,10 +117,8 @@ parse_response_function
token = strtok_r(NULL, " \t", &token_ptr);
if (NULL != token) { res = RES_BAD_ARG; goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
exit:
+ txtrdr_read_line(txtrdr);
return res;
error:
goto exit;
@@ -217,7 +215,9 @@ sphin_sensor_get_response_function
(struct sphin_sensor* sensor,
double* response_function)
{
- if (NULL == sensor){ return RES_BAD_ARG; }
+ if (NULL == sensor || NULL == response_function){
+ return RES_BAD_ARG;
+ }
*response_function = sensor->response_function;
return RES_OK;
diff --git a/src/sphin_source.c b/src/sphin_source.c
@@ -134,6 +134,7 @@ parse_direction_distribution
else{
/* TODO implement parsing of a vector, taking into account the formatting
* in the input file to be decided */
+ res = RES_BAD_ARG; goto error;
}
d3_set(source->direction_distribution.collim.direction, direction);
}
@@ -145,14 +146,12 @@ parse_direction_distribution
res = cstr_to_double(str_collimation_degree, &collimation_degree);
if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
if (collimation_degree < 0) { res = RES_BAD_ARG; goto error; }
+ source->direction_distribution.cos_pow_n.collimation_degree = collimation_degree;
}
else { res = RES_BAD_ARG; goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
-
exit:
+ txtrdr_read_line(txtrdr);
return res;
error:
goto exit;
@@ -176,19 +175,19 @@ parse_flux_density
str_flux_density = strtok_r(value, " \t", &token_ptr);
res = cstr_to_double(str_flux_density, &flux_density);
if (RES_OK != res) { res = RES_BAD_ARG; goto error; }
- if (flux_density< 0) { res = RES_BAD_ARG; goto error; }
+ if (flux_density < 0) { res = RES_BAD_ARG; goto error; }
source->flux_density.flux_density = flux_density;
/* Parse unit */
- flux_density_unit = strtok_r(value, " \t", &token_ptr);
+ flux_density_unit = strtok_r(NULL, " \t", &token_ptr);
if (NULL == flux_density_unit){ res = RES_BAD_ARG; goto error; }
/* 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;
- source->flux_density.flux_density *= 1e-6; /* From mol to umol */
+ source->flux_density.flux_density *= 1e6; /* From mol to umol */
}
else if (0 == strcmp(flux_density_unit, "umol/m^2/s")
|| 0 == strcmp(flux_density_unit, "umol.m^-2.s^-1")) {
@@ -198,22 +197,21 @@ parse_flux_density
/* Energy flux density energy */
else if (0 == strcmp(flux_density_unit, "mW/m^2")
- || 0 == strcmp(flux_density_unit, "mW.s^-1")) {
+ || 0 == strcmp(flux_density_unit, "mW.m^-2")) {
source->flux_density.unit= SPHIN_PHOTON_UNIT_JOULE;
- source->flux_density.flux_density *= 1000;
+ source->flux_density.flux_density *= 1e-3; /* From mWatt to Watt*/
}
else if (0 == strcmp(flux_density_unit, "W/m^2")
- || 0 == strcmp(flux_density_unit, "W.s^-1")) {
+ || 0 == strcmp(flux_density_unit, "W.m^-2")
+ || 0 == strcmp(flux_density_unit, "J.m^-2.s^-1")
+ || 0 == strcmp(flux_density_unit, "J/m^2/s")) {
source->flux_density.unit= SPHIN_PHOTON_UNIT_JOULE;
source->flux_density.flux_density *= 1; /* No conversion */
}
else { res = RES_BAD_ARG; goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
-
exit:
+ txtrdr_read_line(txtrdr);
return res;
error:
goto exit;
@@ -265,10 +263,10 @@ parse_source
if (NULL == token){ res = RES_BAD_ARG; goto error; }
value = token;
if (0 == strcmp(keyword, "flux_density")){
- parse_flux_density(source, txtrdr, value);
+ res = parse_flux_density(source, txtrdr, value);
}
else if (0 == strcmp(keyword, "direction")){
- parse_direction_distribution(source, txtrdr, value);
+ res = parse_direction_distribution(source, txtrdr, value);
}
else {
break;
@@ -316,7 +314,7 @@ sphin_source_get_direction_distribution
(const struct sphin_source* source,
struct sphin_source_direction_distribution* distrib)
{
- if (NULL == source) {
+ if (NULL == source || NULL == distrib) {
return RES_BAD_ARG;
}
@@ -329,7 +327,7 @@ sphin_source_surface_get_flux_density
(const struct sphin_source* source,
struct sphin_source_flux_density* density)
{
- if (NULL == source) {
+ if (NULL == source || NULL == density) {
return RES_BAD_ARG;
}
diff --git a/src/sphin_surface.c b/src/sphin_surface.c
@@ -156,11 +156,8 @@ parse_geometry
res = darray_sphin_geometry_ptr_push_back(&surface->geometries, &geom);
if (RES_OK != res) { goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
-
exit:
+ txtrdr_read_line(txtrdr);
return res;
error:
goto exit;
diff --git a/src/sphin_volume.c b/src/sphin_volume.c
@@ -140,11 +140,8 @@ parse_geometry
res = darray_sphin_geometry_ptr_push_back(&volume->geometries, &geom);
if (RES_OK != res) { goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
-
exit:
+ txtrdr_read_line(txtrdr);
return res;
error:
goto exit;
@@ -176,11 +173,11 @@ parse_ka
if (0 == strcmp(ka_unit, "cm^-1")
|| 0 == strcmp(ka_unit, "1/cm")) {
- volume->ka = ka / 100;
+ volume->ka = ka * 100;
}
else if (0 == strcmp(ka_unit, "mm^-1")
|| 0 == strcmp(ka_unit, "1/mm")) {
- volume->ka = ka / 1000;
+ volume->ka = ka * 1000;
}
else if (0 == strcmp(ka_unit, "m^-1")
|| 0 == strcmp(ka_unit, "1/m")) {
@@ -192,11 +189,8 @@ parse_ka
token = strtok_r(NULL, " \t", &token_ptr);
if (NULL != token) { res = RES_BAD_ARG; goto error; }
- /* Advance one line in the parsing */
- res = txtrdr_read_line(txtrdr);
- if (RES_OK != res) { goto error; }
-
exit:
+ txtrdr_read_line(txtrdr);
return res;
error:
goto exit;
@@ -301,7 +295,7 @@ sphin_volume_get_ka
(struct sphin_volume* volume,
double* ka)
{
- if (NULL == volume) {
+ if (NULL == volume || NULL == ka) {
return RES_BAD_ARG;
}
*ka = volume->ka;
@@ -313,7 +307,7 @@ sphin_volume_get_sensor
(struct sphin_volume* volume,
struct sphin_sensor** sensor)
{
- if (NULL == volume) {
+ if (NULL == volume || NULL == sensor || NULL == volume->sensor ) {
return RES_BAD_ARG;
}
*sensor = volume->sensor;
@@ -325,7 +319,7 @@ sphin_volume_get_geometry_count
(struct sphin_volume* volume,
size_t* ngeometries)
{
- if (NULL == volume) {
+ if (NULL == volume || NULL == ngeometries) {
return RES_BAD_ARG;
}