-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
Potential NULL Pointer Dereference in ngx_timezone_update #564
Comments
Hi,
Technically true, but I'm not sure how likely this is seeing as we're using the value as returned from time(2).
Hmm, this seems wrong. "HH\0" is 3 bytes. So even the current value of 4 is more than enough...
Good change (IMHO)
Fixes ... ;)
Not required.
Good change.
I'm ambivalent about the NULL check here, it really shouldn't happen... with time(NULL), it can't fail and its return value must surely be a valid number of seconds since the epoch...
I don't think the comment is needed.
Is what I would probably do...
|
Analysis of the Argument:
|
From the time(2) Linux man-page
So, time(NULL) can't fail... Maybe it's different on other systems... Then there is
Meh... |
Hello! I analyzed Nginx with Svace static analyzer. It found a potential problem in the code in /src/os/unix/ngx_time.c
Problem Description
The function
ngx_timezone_update
contains a potential runtime error when using thelocaltime
function. Specifically:Potential NULL Pointer Dereference:
localtime
function may returnNULL
if an error occurs or if the input pointer (&s
) is invalid.localtime
returnsNULL
. As a result, passing the returned value (t
) directly tostrftime
could lead to a dereference of aNULL
pointer, causing undefined behavior or a program crash.Buffer Size Issue:
buf
array is defined with a size of 4 bytes, but the%H
format instrftime
generates a string like"HH"
(e.g.,"12"
) plus a null terminator (\0
). This requires at least 3 bytes for the string and 1 byte for the null terminator, totaling 4 bytes. However, it is safer to allocate one extra byte to prevent potential overflow issues.Use of
0
Instead ofNULL
:time(0)
works because0
is interpreted as a null pointer in this context, it is less clear and less portable than explicitly usingNULL
. Modern coding standards recommend usingNULL
for better readability and compatibility.Solution
To address these issues, the following changes were made:
Check for
NULL
Return fromlocaltime
:localtime
to ensure that the returned pointer (t
) is notNULL
.t
isNULL
, log an error message usingngx_log_error
and exit the function early to prevent further execution with invalid data.Increase Buffer Size:
buf
array from4
to5
to ensure there is enough space for the formatted string and the null terminator.Replace
0
withNULL
:time(0)
withtime(NULL)
for better clarity and adherence to modern coding standards.Impact of the Fix
These changes improve the robustness and safety of the code by:
NULL
pointer.localtime
might fail.This patch ensures that the function behaves correctly even in unexpected scenarios, making it more reliable for production use.
Found by Linux Verification Center (linuxtesting.org) with SVACE.
The text was updated successfully, but these errors were encountered: