Skip to content

MDEV-39173 Replace sprintf with snprintf#4869

Open
Pochatkin wants to merge 2 commits intoMariaDB:mainfrom
Pochatkin:MDEV-39173
Open

MDEV-39173 Replace sprintf with snprintf#4869
Pochatkin wants to merge 2 commits intoMariaDB:mainfrom
Pochatkin:MDEV-39173

Conversation

@Pochatkin
Copy link
Copy Markdown

@Pochatkin Pochatkin commented Mar 27, 2026

https://jira.mariadb.org/browse/MDEV-39173

Summary

Remove the unscoped #pragma GCC diagnostic ignored "-Wdeprecated-declarations" from include/violite.h. The pragma was originally added to suppress OpenSSL deprecation warnings on macOS, but is no longer needed as modern macOS builds use bundled WolfSSL or external OpenSSL without deprecated declarations.

The pragma also silently suppressed ~100 "sprintf is deprecated" warnings on macOS. Replace all sprintf/vsprintf calls with snprintf/vsnprintf across the codebase (118 files), excluding vendored third-party code (readline, wolfssl, wsrep-lib, libmariadb, groonga, zlib).

Commits

Commit 1sprintfsnprintf bulk conversion + pragma removal (118 files)

Commit 2 — Address review feedback: use actual buffer sizes instead of recomputed or hardcoded values:

  • client/mysql.cc: Add buff_size parameter to nice_time() and end_timer() to track remaining buffer space instead of hardcoded 32. Save histfile and histfile_tmp allocation sizes in variables instead of recomputing strlen().
  • mysys/mf_getdate.c: Add to_len parameter to get_date() instead of hardcoded GETDATE_BUFF_SIZE, update all 12 callers with actual buffer sizes.
  • sql/rpl_mi.cc: Fix pre-existing bug writing "..." to wrong buffer (dbuff instead of buff).
  • extra/mariabackup, storage/connect: Save PlugSubAlloc/malloc sizes in variables and pass to snprintf.

Buffer size audit (397 snprintf calls)

All 397 introduced snprintf calls were audited for correct buffer sizes. Patterns used:

Pattern Usage Example
sizeof(local_array) ~60% snprintf(buff, sizeof(buff), ...)
Named constant matching declaration ~15% snprintf(errbuf, MYSQL_ERRMSG_SIZE, ...)
Saved allocation size variable ~5% size_t sz = strlen(a)+2; p = malloc(sz); snprintf(p, sz, ...)
end - ptr remaining space ~5% snprintf(buff, buff_end - buff, ...)
sizeof(struct->member) ~5% snprintf(stmt->last_error, sizeof(stmt->last_error), ...)
Function size parameter ~3% void get_date(char *to, ..., size_t to_len)
Hardcoded number matching allocation ~3% snprintf(p, 32, ...) where callers use char buf[32]
Object accessor ~2% snprintf(Q->GetStr(), Q->GetSize(), ...)

Issues found and fixed

File Problem Fix
client/mysql.cc histfile Size recomputed via strlen() 3 times Save in histfile_size variable
client/mysql.cc nice_time() Hardcoded 32 can exceed remaining buffer Add buff_size param, use buff_end - buff
mysys/mf_getdate.c Hardcoded GETDATE_BUFF_SIZE=32, callers pass 17-20 byte buffers Add to_len parameter, update all callers
sql/rpl_mi.cc:1530 Pre-existing bug: "..." written to dbuff + cur_len instead of buff + cur_len Write to buff
extra/mariabackup, storage/connect Sizes recomputed via strlen() Save allocation size in variables

Not modified (vendored code)

extra/readline/, extra/wolfssl/, wsrep-lib/, libmariadb/, storage/mroonga/vendor/groonga/, zlib/, plugin/handler_socket/perl-Net-HandlerSocket/ppport.h

Verification

  • macOS build with -Werror=deprecated-declarations: passed (zero warnings)
  • git grep -w sprintf in own code: zero results (only comments remain)

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 27, 2026

CLA assistant check
All committers have signed the CLA.

@Pochatkin
Copy link
Copy Markdown
Author

@gkodinov Could you please take a look as ticket reporter?

@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 27, 2026
Copy link
Copy Markdown
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for attempting to fix this.
Unfortunately, it's not as simple as letting some AI do it.
You will need to find the actual size of the buffer and then set that as a limit.
In some cases the AI actually managed (mostly when the buffer is a local stack variable). But that's unfortunately not easy in cases where a bigger context is needed.
And it's especially impossible when certain assumptions are made couple of layers up.

Also, please resolve the merge conflicts. And any resulting buildbot failures.

One final thing: please rebase this to 10.11. This was the version decided as the best starting point after some deliberation.

client/mysql.cc Outdated
if (histfile)
{
sprintf(histfile,"%s/.mariadb_history", home);
snprintf(histfile,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not how you do this! It's about the size of the receiving buffer.

@Pochatkin
Copy link
Copy Markdown
Author

@gkodinov Thank you for the review. Pushed a follow-up commit that addresses the feedback:

Buffer size fixes (commit 2):

  • client/mysql.cc: histfile and histfile_tmp now save allocation size in a variable instead of recomputing strlen(). nice_time() and end_timer() now accept a buff_size parameter and track remaining space via buff_end - buff instead of hardcoded 32.
  • mysys/mf_getdate.c: Added to_len parameter to get_date() to replace the hardcoded GETDATE_BUFF_SIZE=32. Updated all 12 callers. This fixes the case where client/mysqldump.cc passes a 20-byte buffer and mysys/my_redel.c passes a 17-byte buffer.
  • sql/rpl_mi.cc: Fixed a pre-existing bug where "..." was written to dbuff + cur_len instead of buff + cur_len (cur_len tracks position in buff, not dbuff).
  • extra/mariabackup, storage/connect: Saved PlugSubAlloc/malloc sizes in variables.

Full audit: All 397 snprintf calls were audited. Details in PR description.

Regarding rebasing to 10.11 — I'll do that once the review converges, to avoid repeated conflict resolution.

…f with snprintf

Remove the unscoped #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
from include/violite.h. The pragma was originally added to suppress OpenSSL
deprecation warnings on macOS, but is no longer needed as modern macOS builds
use bundled WolfSSL or external OpenSSL without deprecated declarations.

The pragma also silently suppressed ~100 "sprintf is deprecated" warnings on
macOS. Replace all sprintf/vsprintf calls with snprintf/vsnprintf across the
codebase (118 files), excluding vendored third-party code (readline, wolfssl,
wsrep-lib, libmariadb, groonga, zlib).
… values

- client/mysql.cc: Add buff_size parameter to nice_time() and end_timer()
  to track remaining buffer space instead of hardcoded 32. Save histfile
  and histfile_tmp allocation sizes in variables instead of recomputing
  strlen().
- mysys/mf_getdate.c: Add to_len parameter to get_date() instead of
  hardcoded GETDATE_BUFF_SIZE, update all callers with actual buffer sizes.
- sql/rpl_mi.cc: Fix pre-existing bug writing "..." to wrong buffer
  (dbuff instead of buff).
- extra/mariabackup, storage/connect: Save PlugSubAlloc/malloc sizes in
  variables and pass to snprintf.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

3 participants