phoenix-rtos / phoenix-rtos-project

Sample project using Phoenix-RTOS
https://phoenix-rtos.com
BSD 3-Clause "New" or "Revised" License
43 stars 32 forks source link

Scanf Inf and Nan handling problem #634

Closed maska989 closed 1 year ago

maska989 commented 1 year ago

During scanf tests there was a problem reading data assigned as INF and NAN

The problem occurs in an all-conversion specifier wich can handle that (%aAeEgGfF).

There is posix requirements:

int main(void) { float val_float;

sscanf("inf", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("infinity", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("InF", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("INFINITY", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("InFiNiTy", "%f", &val_float);
printf("\n%f\n", val_float);

sscanf("nan", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("NAn", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("NaN", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("nAN", "%f", &val_float);
printf("\n%f\n", val_float);
sscanf("nAn", "%f", &val_float);
printf("\n%f\n", val_float);

}


 - Outcome on Host-generic-pc and ia32-generic-qemu:

![Screenshot from 2023-03-07 13-30-36](https://user-images.githubusercontent.com/49757239/223423957-63c4dba8-93ca-41a9-bbaa-4c6e25e430de.png)
gerard5 commented 1 year ago

The scanf() implementation uses three functions strtof(), strtod(), strtold() to read and convert float/double/long double values. So the root problem is not in scanf() itself, but in the limited implementation of strtold() which is common base to the other two functions.

As I write in comment for https://github.com/phoenix-rtos/phoenix-rtos-project/issues/637#issuecomment-1460379655

maska989 commented 1 year ago

@gerard5 you were right, there is problem with strtod.

int main(void) { char *outputStrtod[15]; float outputScanf; printf("\n\nStrtod =>\n"); printf("\n%f\n", strtod("inf", outputStrtod)); printf("\n%f\n", strtod("infinity", outputStrtod)); printf("\n%f\n", strtod("nan", outputStrtod));

printf("\n\nScanf =>\n");
sscanf("inf", "%f", &outputScanf);
printf("\n%f\n", outputScanf);
sscanf("infinity", "%f", &outputScanf);
printf("\n%f\n", outputScanf);
sscanf("nan", "%f", &outputScanf);
printf("\n%f\n", outputScanf);

}



* outcome: 
![Screenshot from 2023-03-13 12-34-58](https://user-images.githubusercontent.com/49757239/224691824-b87cd03b-a391-4c64-8512-a12c992b0fde.png)