-
-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stop using atoi()/atol()/strtol(), convert all to str_to_*() and str_to_*_strict() #966
Labels
C-str
Issues and PRs about C/C++ methods, headers and data types dealing with strings and memory blocks
refactor/fightwarn
PR or issue proposal to improve code maintainability without functional changes, or to fix warnings
Comments
biergaizi
added a commit
to biergaizi/nut
that referenced
this issue
Jan 25, 2021
jimklimov
added
the
refactor/fightwarn
PR or issue proposal to improve code maintainability without functional changes, or to fix warnings
label
Nov 14, 2021
jimklimov
added
the
C-str
Issues and PRs about C/C++ methods, headers and data types dealing with strings and memory blocks
label
Apr 10, 2024
jimklimov
added a commit
to jimklimov/nut
that referenced
this issue
Jul 22, 2024
… reinventing the wheel [networkupstools#266, networkupstools#966] Signed-off-by: Jim Klimov <[email protected]>
jimklimov
added a commit
to jimklimov/nut
that referenced
this issue
Jul 22, 2024
… reinventing the wheel [networkupstools#266, networkupstools#966] Signed-off-by: Jim Klimov <[email protected]>
jimklimov
added a commit
to jimklimov/nut
that referenced
this issue
Jul 23, 2024
… reinventing the wheel [networkupstools#266, networkupstools#966] Signed-off-by: Jim Klimov <[email protected]>
jimklimov
added a commit
to jimklimov/nut
that referenced
this issue
Jul 23, 2024
… reinventing the wheel [networkupstools#266, networkupstools#966] Signed-off-by: Jim Klimov <[email protected]>
jimklimov
added a commit
to jimklimov/nut
that referenced
this issue
Jul 23, 2024
… reinventing the wheel [networkupstools#266, networkupstools#966] Signed-off-by: Jim Klimov <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
C-str
Issues and PRs about C/C++ methods, headers and data types dealing with strings and memory blocks
refactor/fightwarn
PR or issue proposal to improve code maintainability without functional changes, or to fix warnings
In the C programming language,
atoi()
andatol()
don't check errors. If the input string is invalid, their behavior is undefined with no guaranteed return value (a particular implementation of libc may have defined return value, but it's not guaranteed by the C standard).strtol()
is better, but its error checking is complicated, one need to test three conditions,errno != 0
for overflows and invalid integers,nptr != endptr
for strings without digits, and*endptr != '\0'
for strings mixed with letters and numbers, it is easily misused. Also, it ignores the leading space in the string that can hide bugs. *BSD'sstrtonum()
is the best function for this job, but it's not portable.To solve this problem, the NUT project already has a builtin library
common/str.c
that provides easy-to-use and portable string conversion functions with robust error handling, such asstr_to_short()
,str_to_ushort()
,str_to_long()
(they are implemented by callingstrtol()
with correct error checking), "strict" versions that report errors if there are leading spaces also exist. Unfortunately, these good string functions is used by almost nobody. They are only used by 3 drivers, and it's not used in server code at all.All
atoi()
,atol()
,strtol()
in the existing code should be converted to usestr_to_*()
instead, if it's not practical to convert existing code, at least they should be disallowed in new code. Ideally, the "strict" version of the functions should be preferred.The
new-drivers.txt
document should inform the developers about the existence of these functions.The text was updated successfully, but these errors were encountered: