Skip to content

Conversation

alexandre-daubois
Copy link
Member

Internals book explicitly specifies that sprintf() should be avoided where possible for safer alternatives.

@alexandre-daubois alexandre-daubois marked this pull request as ready for review August 26, 2025 17:53
@@ -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),
Copy link
Member

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);
Copy link
Member

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);
Copy link
Member

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,
Copy link
Member

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.

@nielsdos
Copy link
Member

I did a round on this once a long time ago, strange I missed these, thanks for catching this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants