Skip to content
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

Convert instances of sprintf to snprintf in sql #3075

Draft
wants to merge 2 commits into
base: 11.5
Choose a base branch
from

Conversation

tgross35
Copy link
Contributor

Remove use of sprintf in favor of snprintf where indicated by deprecation warnings.

  • The Jira issue number for this PR is: MDEV-______

Description

This is a portion of #2958

Release Notes

None needed

How can this PR be tested?

Compile with -Wdeprecated-declarations (I believe it is enabled by default for the relevant files) and use a version of libc that marks sprintf deprecated.

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.
  • This is a bug fix and the PR is based against the earliest maintained branch in which the bug can be reproduced.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

@dlenski
Copy link
Contributor

dlenski commented Feb 20, 2024

This is a portion of #2958

Per #2958 (comment), you've split this up into 3 smaller pieces in order to narrow down the source of the CI failures.

sql/log_event.h Outdated
Comment on lines 2910 to 2911
// TODO: use real value, struggling with imports here
snprintf(c+1, (buf + buf_len) - (c + 1), ",%lu", fmt);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the "real value" that you're struggling to obtain here?

Comment on lines 2851 to 2854
char *str_end = to->str + to->length;
size_t add_to_len;

str += (to->length= sprintf(str, fmt_frag, 0));
str += (to->length= snprintf(str, str_end-str, fmt_frag, 0));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think that str_end-str is easier to understand here than simply using to->length? I do not.

My preference would be something like…

size_t str_length = to->length;
…
snprintf(str, str_length, …)

Comment on lines 151 to 161
lnbuf+= snprintf(buf + lnbuf, sizeof(buf) - lnbuf,
"%d\t", slice->thread);
lnbuf+= snprintf(buf + lnbuf, sizeof(buf) - lnbuf,
"%s\t", gcalc_ev_name(slice->event));

lnbuf+= gcalc_pi_str(buf + lnbuf, slice->pi, "\t");
lnbuf+= gcalc_pi_str(buf + lnbuf, sizeof(buf) - lnbuf, slice->pi, "\t");
if (slice->is_bottom())
lnbuf+= sprintf(buf+lnbuf, "bt\t");
lnbuf+= snprintf(buf+lnbuf, sizeof(buf) - lnbuf, "bt\t");
else
lnbuf+= gcalc_pi_str(buf+lnbuf, slice->next_pi, "\t");
lnbuf+= gcalc_pi_str(buf + lnbuf, sizeof(buf) - lnbuf,
slice->next_pi, "\t");
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to include these changes as part of this PR? They don't appear to be directly related to the goal of bounds-checking sprintf invocations.

It appears that you're actually adding new fields to each line printed here.

@tgross35 tgross35 marked this pull request as draft February 25, 2024 03:26
@tgross35 tgross35 force-pushed the remove-sprintf-sql branch 3 times, most recently from 8d1e1c0 to 865eb55 Compare February 28, 2024 07:58
This adds an abstraction for the commonly used pointer-as-cursor
interface, with checked operations. It is meant to simplify sequential
operations of `sprintf` and `strcat` to a preallocated buffer.

Signed-off-by: Trevor Gross <[email protected]>
@tgross35
Copy link
Contributor Author

tgross35 commented Apr 5, 2024

@LinuxJedi since you have been reviewing the other sprintf PRs, would you mind giving some thoughts on https://github.com/MariaDB/server/blob/e776115ebb723f4280573886363c93daa77ed342/include/bufcursor.h / https://github.com/MariaDB/server/blob/e776115ebb723f4280573886363c93daa77ed342/sql/bufcursor.c included as part of this PR? It is meant as an easy API to replace the pattern of sprintfing or strcpying something and then incrementing a cursor. It just asserts if there isn't enough room.

I added this because that pattern is used frequently (the rest of this PR and sometimes in https://github.com/MariaDB/server/pull/3076/files). Switching directly to snprintf in these cases is messy - keeping the math correct while handling errors seems too error prone to use directly, IMO.

@LinuxJedi
Copy link
Contributor

@tgross35 what would that provide us over using the String class in sql_string.h?

@tgross35
Copy link
Contributor Author

@tgross35 what would that provide us over using the String class in sql_string.h?

Not enough to be special, I can update it to use that

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