MDEV-39173 Replace sprintf with snprintf#4869
MDEV-39173 Replace sprintf with snprintf#4869Pochatkin wants to merge 2 commits intoMariaDB:mainfrom
Conversation
|
@gkodinov Could you please take a look as ticket reporter? |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
That's not how you do this! It's about the size of the receiving buffer.
|
@gkodinov Thank you for the review. Pushed a follow-up commit that addresses the feedback: Buffer size fixes (commit 2):
Full audit: All 397 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.
https://jira.mariadb.org/browse/MDEV-39173
Summary
Remove the unscoped
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"frominclude/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/vsprintfcalls withsnprintf/vsnprintfacross the codebase (118 files), excluding vendored third-party code (readline, wolfssl, wsrep-lib, libmariadb, groonga, zlib).Commits
Commit 1 —
sprintf→snprintfbulk conversion + pragma removal (118 files)Commit 2 — Address review feedback: use actual buffer sizes instead of recomputed or hardcoded values:
client/mysql.cc: Addbuff_sizeparameter tonice_time()andend_timer()to track remaining buffer space instead of hardcoded 32. Savehistfileandhistfile_tmpallocation sizes in variables instead of recomputingstrlen().mysys/mf_getdate.c: Addto_lenparameter toget_date()instead of hardcodedGETDATE_BUFF_SIZE, update all 12 callers with actual buffer sizes.sql/rpl_mi.cc: Fix pre-existing bug writing"..."to wrong buffer (dbuffinstead ofbuff).extra/mariabackup,storage/connect: SavePlugSubAlloc/mallocsizes in variables and pass tosnprintf.Buffer size audit (397 snprintf calls)
All 397 introduced
snprintfcalls were audited for correct buffer sizes. Patterns used:sizeof(local_array)snprintf(buff, sizeof(buff), ...)snprintf(errbuf, MYSQL_ERRMSG_SIZE, ...)size_t sz = strlen(a)+2; p = malloc(sz); snprintf(p, sz, ...)end - ptrremaining spacesnprintf(buff, buff_end - buff, ...)sizeof(struct->member)snprintf(stmt->last_error, sizeof(stmt->last_error), ...)void get_date(char *to, ..., size_t to_len)snprintf(p, 32, ...)where callers usechar buf[32]snprintf(Q->GetStr(), Q->GetSize(), ...)Issues found and fixed
client/mysql.cchistfilestrlen()3 timeshistfile_sizevariableclient/mysql.ccnice_time()buff_sizeparam, usebuff_end - buffmysys/mf_getdate.cGETDATE_BUFF_SIZE=32, callers pass 17-20 byte buffersto_lenparameter, update all callerssql/rpl_mi.cc:1530"..."written todbuff + cur_leninstead ofbuff + cur_lenbuffextra/mariabackup,storage/connectstrlen()Not modified (vendored code)
extra/readline/,extra/wolfssl/,wsrep-lib/,libmariadb/,storage/mroonga/vendor/groonga/,zlib/,plugin/handler_socket/perl-Net-HandlerSocket/ppport.hVerification
-Werror=deprecated-declarations: passed (zero warnings)git grep -w sprintfin own code: zero results (only comments remain)