-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Avoid using unsafe sprintf()
#19598
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
base: master
Are you sure you want to change the base?
Avoid using unsafe sprintf()
#19598
Conversation
@@ -90,7 +90,10 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */ | |||
} | |||
} | |||
|
|||
p += sprintf(env_value + p + socket_set_buf, "%s%s=%s", (p && !socket_set_buf) ? "," : "", ls->key, fd); | |||
int written = snprintf(env_value + p + socket_set_buf, | |||
strlen(ls->key) + strlen(fd) + 2 + (p && !socket_set_buf ? 1 : 0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to store the length in a variable, and then subtract p + socket_set_buf
from it.
@@ -386,7 +386,7 @@ ZEND_COLD void zend_debug_alloc_output(char *format, ...) | |||
va_list args; | |||
|
|||
va_start(args, format); | |||
vsprintf(output_buf, format, args); | |||
vsnprintf(output_buf, sizeof(output_buf), format, args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire function looks to be unused, if I'm not mistaken. Maybe we can just drop it.
@@ -130,7 +130,7 @@ static int get_formatted_timestamp_tz(pdo_stmt_t *stmt, const ISC_TIMESTAMP_TZ* | |||
return 1; | |||
} | |||
|
|||
size_t timestamp_tz_len = sprintf(timestampTzBuf, "%s %s", timestampBuf, timeZoneBuffer); | |||
size_t timestamp_tz_len = snprintf(timestampTzBuf, sizeof(timestampTzBuf), "%s %s", timestampBuf, timeZoneBuffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably just be zend_strpprintf() to directly print into the zend_string.
@@ -90,7 +90,10 @@ static void fpm_sockets_cleanup(int which, void *arg) /* {{{ */ | |||
} | |||
} | |||
|
|||
p += sprintf(env_value + p + socket_set_buf, "%s%s=%s", (p && !socket_set_buf) ? "," : "", ls->key, fd); | |||
int written = snprintf(env_value + p + socket_set_buf, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, the smart_string API would avoid manually reallocating a buffer.
I did a round on this once a long time ago, strange I missed these, thanks for catching this |
Internals book explicitly specifies that
sprintf()
should be avoided where possible for safer alternatives.