[BUG] readModelParams: Segfault on missing parameter value and unvalidated strtod silently accepts non-numeric strings
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 68/100
Direzione di ricerca
Inizia da src/common/modelParams.c, in particolare da readModelParams() in corrispondenza della tokenizzazione e delle chiamate a strtod(); confronta la gestione dei valori mancanti in src/sipnet/frontend.c:83-88. Poi esamina tests/sipnet/test_sipnet_infrastructure/testNonfiniteParams.c per individuare il modello di test esistente. Il lavoro è concluso quando i valori mancanti e le stringhe numeriche malformate producono gli errori specificati e i test coprono entrambi i casi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
In src/common/modelParams.c:readModelParams(), the .param file reader contains two input validation flaws:
- Segmentation Fault (SIGSEGV): When a parameter record has a parameter name without a corresponding value,
strtok()returnsNULL, and passing it intostrcpy()causes an immediate segmentation fault (signal 11 / exit code 139). - Silent Acceptance of Invalid Strings: When a parameter value contains non-numeric characters (e.g.,
plantWoodCInit not_a_numberorsoilRespQ10 2.2abc),value = strtod(strValue, &errc)parses the string, buterrcis never validated. As a result,strtod()returns0.0(or2.2), and SIPNET silently accepts the corrupted parameter value and runs with exit code 0.
Detailed Problem & Root Cause
1. Crash on Missing Parameter Value
In src/common/modelParams.c:170-172:
// tokenize line:
strcpy(pName, strtok(line, SEPARATORS)); // copy first token into pName
strcpy(strValue, strtok(NULL, SEPARATORS));
When a line in the .param file contains only a parameter name without a following value (for example, plantWoodCInit alone on a line, or trailing whitespace without a value token):
strtok(line, SEPARATORS)returns the parameter name.strtok(NULL, SEPARATORS)returnsNULL.strcpy(strValue, NULL)invokes undefined behavior and immediately causes a Segmentation fault (core dumped).
In contrast, src/sipnet/frontend.c:83-88 explicitly checks:
if (inputValue == NULL) {
logError("Error in input file: No value given for input item %s\n", inputName);
logError("Please fix %s and re-run\n", ctx.inputFile);
exit(EXIT_CODE_BAD_PARAMETER_VALUE);
}
2. Silent Acceptance of Non-Numeric Parameter Values
In src/common/modelParams.c:149, 180:
char *errc;
...
value = strtod(strValue, &errc);
While errc is passed to strtod(), it is never checked afterwards.
According to the C standard (strtod(3)), if no conversion can be performed, strtod returns 0.0 and sets *errc = strValue. If conversion stops partway through (e.g., 12.5xyz), *errc != '\0'.
Because errc is completely ignored, invalid strings are silently converted to 0.0 (or truncated values), and the model proceeds without any warning or error, silently compromising scientific simulation results.
Steps to Reproduce
Reproduction 1: Segmentation Fault
- Add an empty parameter line to
sipnet.param:echo "plantWoodCInit" >> tests/smoke/niwot/sipnet.param - Run SIPNET:
./sipnet -i tests/smoke/niwot/sipnet.in - Process crashes with
SIGSEGV(exit code 139):Program received signal SIGSEGV, Segmentation fault. __strcpy_avx2 () at ../sysdeps/x86_64/multiarch/strcpy-avx2.S:80 #0 __strcpy_avx2 () #1 0x0000555555575bcb in readModelParams (modelParams=0x555555586d60, paramFile=0x555555586b80) at src/common/modelParams.c:171 #2 0x0000555555557970 in readParamData (modelParamsPtr=0x7fffffffd2c8, paramFile=0x7fffffffd510 "sipnet.param") at src/sipnet/sipnet.c:403 #3 0x000055555555aff4 in initModel (modelParams=0x7fffffffd2c8, paramFile=0x7fffffffd510 "sipnet.param", climFile=0x7fffffffd610 "sipnet.clim") at src/sipnet/sipnet.c:1993 #4 0x00005555555655ca in main (argc=1, argv=0x7fffffffda58) at src/sipnet/frontend.c:212
Reproduction 2: Silent Conversion of Non-Numeric String
- Edit any parameter in
sipnet.paramto a non-numeric string:sed -i 's/^plantWoodCInit .*/plantWoodCInit not_a_number/' sipnet.param - Run SIPNET:
./sipnet - Outcome: SIPNET completes with
Exit code: 0, silently treatingplantWoodCInitas0.0.
Proposed Solution
-
Guard
strtokreturn value:
Check if the value token isNULLbefore copying:char *pNameToken = strtok(line, SEPARATORS); char *valToken = strtok(NULL, SEPARATORS); if (pNameToken == NULL) { continue; // empty line / comment } if (valToken == NULL) { logError("Error in parameter file: No value given for parameter %s\n", pNameToken); exit(EXIT_CODE_INPUT_FILE_ERROR); } strncpy(pName, pNameToken, sizeof(pName) - 1); pName[sizeof(pName) - 1] = '\0'; strncpy(strValue, valToken, sizeof(strValue) - 1); strValue[sizeof(strValue) - 1] = '\0'; -
Validate
errcfromstrtod:
Verify that the entire value string was consumed as a valid number:value = strtod(strValue, &errc); if (errc == strValue || *errc != '\0') { logError("parameter %s has invalid numeric value: '%s'\n", pName, strValue); exit(EXIT_CODE_BAD_PARAMETER_VALUE); } -
Add Unit Tests:
Extendtests/sipnet/test_sipnet_infrastructure/testNonfiniteParams.c(or add cases to a new unit test suite) verifying that:- Parameters with missing values exit with
EXIT_CODE_INPUT_FILE_ERROR. - Parameters with non-numeric strings (
"not_a_number","12.3foo", etc.) exit withEXIT_CODE_BAD_PARAMETER_VALUE.
- Parameters with missing values exit with
- Lingua principale
- C
- Stelle
- 42
- Fork
- 38
- Merge medio
- 3g 21h
- PR unite (30g)
- 10
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di PecanProject/sipnet
-
Make sure that documentation of sipnet.out header reflects implementationForse già presa @TianHengZhuang l’ha presa 19 giorni fa. Apertadocumentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
PecanProject/sipnet#345 · 8 commenti · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
Remove unused `Tracker` fieldsForse di nuovo libera @ANAMASGARD l’ha presa 9 giorni fa e non c’è nessuna pull request aperta. Apertagood first issue on hold
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
PecanProject/sipnet#430 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 2 giorni
-
Event checks ignore every events.in record after the firstForse già presa @ANAMASGARD l’ha presa 9 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 70/100
PecanProject/sipnet#429 · 3 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Events: revamp handling of "big delta" events to post-pool-updateForse già presa @Alomir l’ha presa 11 giorni fa. Aperta
PecanProject/sipnet#423 · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Missed event across a year boundary silently blocks later eventsForse già presa @ANAMASGARD l’ha presa 11 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 73/100
PecanProject/sipnet#421 · 4 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di PecanProject/sipnet
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
msys2/MINGW-packages#33356 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
kmod-project/kmod#492 ·
I maintainer di solito rispondono entro 4 giorni
-
SDL_GPU Vulkan: wrong screen orientation on OpenHarmony (preTransform set without pre-rotation)Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
libsdl-org/SDL#16670 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
-
要トリアージ 辞書
Difficoltà 1/5 1-3 ore Idoneità per principianti 86/100
yksr-melt/Meltype#1625 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno