zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.12k stars 6.21k forks source link

libc: implement `gets()` #66945

Open ycsin opened 6 months ago

ycsin commented 6 months ago

This was originally added as a ticket under POSIX, but gets() is not a POSIX function. It is from C89.

https://en.cppreference.com/w/c/io/gets

The expectation from the POSIX API is that this function is implemented as part of whatever C library is in use.

cfriedt commented 6 months ago

This API call is deprecated / unsafe.

The man pages say never use it and to use fgets instead.

https://man7.org/linux/man-pages/man3/gets.3.html

It might be worth making a note that this call is simply not supported in the documentation.

Likely picolibc / newlib has it though... 🤔

Wouldn't mind a second opinion from @keith-packard or @stephanosio

keith-packard commented 6 months ago

glibc follows c11/c17 standards and doesn't even define gets unless you build with an older C standard (gcc -std=c99 provides gets). Even in that case, the API is marked as deprecated and you get a warning at compile time. And another warning at link time. Even building with _POSIX_C_SOURCE=200809L leaves gets undefined without also using the older ISO C standard.

This seems like a reasonable plan to me. Applications that want gets newline semantics (which differ from fgets, of course), should use the new ISO C gets_s API instead.

Picolibc and newlib don't provide gets_s, which should probably get fixed.