commit 35b543f3fbd7d5858fbc169c4095ae2a4fd52303
parent 9377f60fb37a662908093b216f971f07c0dd1945
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 5 Feb 2026 15:28:10 +0100
Check the return value of fprintf in mixture tests
Several tests use a memory buffer as file storage. Its content is
defined by writing data that could exceed the buffer size, causing a
write error. Hence these checks.
Note that the return value of the fprintf function is not checked when
the file is a temporary file. Here, we assume that once successfully
created, writing to such a file cannot fail.
Diffstat:
1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/src/test_sln_mixture.c b/src/test_sln_mixture.c
@@ -164,7 +164,6 @@ test_invalid_molecule
char buf[1024] = {0};
FILE* fp;
-
/* Note that the comment char will be added as the last character written to
* the file in order to fill the rest of the file with a comment */
CHK(fp = fmemopen(buf, sizeof(buf), "w+"));
@@ -177,70 +176,70 @@ test_invalid_molecule
/* Name is missing */
RESET;
- fprintf(fp, "0.3 25\n#");
+ CHK(fprintf(fp, "0.3 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Name is invalid */
RESET;
- fprintf(fp, "Water 0.3 25\n#");
+ CHK(fprintf(fp, "Water 0.3 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Name is valid but is not in the isotope metadata */
RESET;
- fprintf(fp, "O2 0.1 25\n#");
+ CHK(fprintf(fp, "O2 0.1 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Definition of duplicate molecule */
RESET;
- fprintf(fp, "H2O 0.3 25\n");
- fprintf(fp, "H2O 0.1 50\n#");
+ CHK(fprintf(fp, "H2O 0.3 25\n") > 0);
+ CHK(fprintf(fp, "H2O 0.1 50\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Concentration is missing */
RESET;
- fprintf(fp, "H2O 25\n#");
+ CHK(fprintf(fp, "H2O 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Invalid concentration */
RESET;
- fprintf(fp, "H2O -0.1 25\n#");
+ CHK(fprintf(fp, "H2O -0.1 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
RESET;
- fprintf(fp, "H2O 1.1 25\n#");
+ CHK(fprintf(fp, "H2O 1.1 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Missing cutoff */
RESET;
- fprintf(fp, "H2O 0.3\n#");
+ CHK(fprintf(fp, "H2O 0.3\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Invalid cutoff */
RESET;
- fprintf(fp, "H2O 0.3 0\n#");
+ CHK(fprintf(fp, "H2O 0.3 0\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* Invalid overall cocentration */
RESET;
- fprintf(fp, "H2O 0.3 25\n");
- fprintf(fp, "CO2 0.7 50\n");
- fprintf(fp, "O3 0.1 25\n#");
+ CHK(fprintf(fp, "H2O 0.3 25\n") > 0);
+ CHK(fprintf(fp, "CO2 0.7 50\n") > 0);
+ CHK(fprintf(fp, "O3 0.1 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_BAD_ARG);
/* An overall concentration < 1 is valid */
RESET;
- fprintf(fp, "H2O 0.2 25\n");
- fprintf(fp, "CO2 0.3 50\n");
- fprintf(fp, "O3 0.4 25\n#");
+ CHK(fprintf(fp, "H2O 0.2 25 # Comment\n") > 0);
+ CHK(fprintf(fp, "CO2 0.3 50\n") > 0);
+ CHK(fprintf(fp, "O3 0.4 25\n#") > 0);
rewind(fp);
CHK(sln_mixture_load(sln, &args, &mixture) == RES_OK);
CHK(sln_mixture_ref_put(mixture) == RES_OK);