From e3b5bb4dfc3705367a71d4b58831190124edf198 Mon Sep 17 00:00:00 2001 From: Mathieu Faverge <mathieu.faverge@inria.fr> Date: Wed, 27 Mar 2024 14:00:45 +0100 Subject: [PATCH] Fix issue #128 by checking more carefully the sscanf error --- include/chameleon/getenv.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/include/chameleon/getenv.h b/include/chameleon/getenv.h index 7f4fd3a20..74c8ecc87 100644 --- a/include/chameleon/getenv.h +++ b/include/chameleon/getenv.h @@ -28,6 +28,7 @@ #include <string.h> #include <stdlib.h> #include <stdio.h> +#include <errno.h> #ifdef __cplusplus extern "C" { @@ -103,13 +104,24 @@ chameleon_env_on_off( char * str, int default_value ) { } static inline int -chameleon_getenv_get_value_int(char * string, int default_value) { +chameleon_getenv_get_value_int( char * string, int default_value ) { + extern int errno; long int ret; - char *str = chameleon_getenv(string); - if (str == NULL) return default_value; + int rc; + char *str = chameleon_getenv(string); - if ( sscanf( str, "%ld", &ret ) != 1 ) { - perror("sscanf"); + if ( str == NULL ) { + return default_value; + } + + rc = sscanf( str, "%ld", &ret ); + if ( rc != 1 ) { + if ( ( rc == EOF ) && ( errno != 0 ) ) { + perror( "chameleon_getenv_get_value_int(sscanf)" ); + } + else { + fprintf( stderr, "%s: env variable %s expects an int value\n", __func__, string ); + } return default_value; } -- GitLab