Skip to content
/ server Public

MDEV-39123: Replace sprintf with snprintf in the InnoDB binlog code#4824

Open
gkodinov wants to merge 1 commit into12.3from
main-MDEV-39123
Open

MDEV-39123: Replace sprintf with snprintf in the InnoDB binlog code#4824
gkodinov wants to merge 1 commit into12.3from
main-MDEV-39123

Conversation

@gkodinov
Copy link
Member

sprintf is deprecated (and triggers a deprecation warning) on some platforms, saying it needs to be replaced with snprintf.
This is a good idea, since sprintf doesn't explicitly check the size of the buffer it is printing to.
Fixed by adding an extra size argument to the helper function and making sure this extra argument is passed down from the caller to snprintf.

@gkodinov gkodinov added the MariaDB Foundation Pull requests created by MariaDB Foundation label Mar 19, 2026

static inline void
binlog_name_make_short(char *name_buf, uint64_t file_no)
binlog_name_make_short(char *name_buf, size_t sizeof_name_buf, uint64_t file_no)
Copy link
Member

Choose a reason for hiding this comment

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

sizeof_name_buf, really? maybe there is something more natural, and short, "buf_size"?

Copy link
Member Author

Choose a reason for hiding this comment

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

used name_buf_size.

@sanja-byelkin
Copy link
Member

see bb-10.11-mac and Marko aproaches in the correspondent MDEV

@CLAassistant
Copy link

CLAassistant commented Mar 19, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Georgi (Joro) Kodinov seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@gkodinov gkodinov requested a review from dr-m March 19, 2026 14:38
Comment on lines 4972 to 4977
char filename[BINLOG_NAME_MAX_LEN];
binlog_name_make_short(filename, file_no);
binlog_name_make_short(filename, sizeof(filename), file_no);
if (purge_info.nonpurge_reason)
sql_print_information("InnoDB: Binlog file %s could not be purged "
"because %s",
filename, purge_info.nonpurge_reason);
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is a simple wrapper of snprintf(). What happens if more than BINLOG_NAME_MAX_LEN characters of input is available? Would the filename be terminated by \0, or could the subsequent sql_print_information() call exceed the bounds of the filename buffer. I tested this, because the documentation of snprintf(3) on my system is a little unclear:

#include <stdio.h>
int main()
{
  char buf[2];
  snprintf(buf, sizeof buf, "foo");
  return printf("hello %s\n", buf);
}

On my system, this would display hello f, so this appears to be fine.

There used to be an issue on Microsoft Windows here, but as noted in https://stackoverflow.com/questions/7706936/is-snprintf-always-null-terminating it was corrected some 10 years ago by implementing the standard snprintf() function.

Copy link
Member Author

Choose a reason for hiding this comment

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

https://linux.die.net/man/3/snprintf says:

The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str.

Comment on lines +4391 to +4393
static_assert(BINLOG_NAME_MAX_LEN <= FN_REFLEN,
"FN_REFLEN too shot to hold InnoDB binlog name");
binlog_name_make_short(name, file_no);
binlog_name_make_short(name, FN_REFLEN, file_no);
Copy link
Contributor

Choose a reason for hiding this comment

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

You could shoot at the static_assert message as well and correct the shot to short. Given that this branch includes cacaaeb, we could use the unary form of static_assert and reduce the source code line count by 1.

Copy link
Member Author

Choose a reason for hiding this comment

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

Please elaborate.

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a typo "too shot" in the static_assert message. You could just write

  static_assert(BINLOG_NAME_MAX_LEN <= FN_REFLEN);

because this branch uses a C++ standard version that supports the unary variant of static_assert.

Copy link
Member Author

Choose a reason for hiding this comment

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

a bit outside of the scope of the fix, but OK. fixing.

Comment on lines +5049 to +5050
binlog_name_make_short(purge_info->nonpurge_filename,
sizeof(purge_info->nonpurge_filename), file_no);
Copy link
Contributor

Choose a reason for hiding this comment

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

The parentheses after sizeof are only needed when the argument is a name of a type. Here it is an expression, and therefore the parentheses are redundant.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will remove the parentheses. But, I was wondering (since this is not wrong as is), is there any coding style description that was violated? Or is this just a personal preference?

@gkodinov gkodinov force-pushed the main-MDEV-39123 branch 2 times, most recently from fa26f3c to 33c9d05 Compare March 23, 2026 09:42
@gkodinov gkodinov changed the base branch from main to 12.3 March 23, 2026 09:44
 binlog code

sprintf is deprecated (and triggers a deprecation warning) on some platforms,
saying it needs to be replaced with snprintf.
This is a good idea, since sprintf doesn't explicitly check the size of the buffer
it is printing to.
Fixed by adding an extra size argument to the helper function and making sure
this extra argument is passed down from the caller to snprintf.
@gkodinov gkodinov requested a review from vaintroub March 23, 2026 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

MariaDB Foundation Pull requests created by MariaDB Foundation

Development

Successfully merging this pull request may close these issues.

5 participants