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

Minimize unsafe C functions usage - replace strcat() and strcpy() - continued #2516

Merged
merged 1 commit into from
May 17, 2024

Conversation

robinnewhouse
Copy link
Contributor

Description

567b681 introduced safe_strcpy() to minimize the use of C with
potentially unsafe memory overflow with strcpy() whose use is
discouraged.
Replace instances of strcpy() with safe_strcpy() where possible, limited
here to files in the sql/ directory.

How can this PR be tested?

All passing MTR tests still pass. Tested manually and in CI.

Basing the PR against the correct MariaDB version

  • 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

Copyright

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer
Amazon Web Services, Inc.

@CLAassistant
Copy link

CLAassistant commented Mar 2, 2023

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.
You have signed the CLA already but the status is still pending? Let us recheck it.

@robinnewhouse robinnewhouse changed the title Minimize unsafe C functions usage - replace strcat() and strcpy() - continued WIP: Minimize unsafe C functions usage - replace strcat() and strcpy() - continued Mar 6, 2023
@robinnewhouse
Copy link
Contributor Author

Marked as WIP. Investigating failing MTR tests.

@ottok ottok marked this pull request as draft March 18, 2023 21:07
@robinnewhouse robinnewhouse force-pushed the 10.4-safe-strcpy branch 2 times, most recently from 5f70ae2 to 9f8f14a Compare March 22, 2023 00:24
@robinnewhouse robinnewhouse marked this pull request as ready for review March 22, 2023 00:24
@robinnewhouse robinnewhouse force-pushed the 10.4-safe-strcpy branch 2 times, most recently from 7fbddc3 to b88b393 Compare April 3, 2023 21:48
@robinnewhouse robinnewhouse changed the title WIP: Minimize unsafe C functions usage - replace strcat() and strcpy() - continued Minimize unsafe C functions usage - replace strcat() and strcpy() - continued Apr 9, 2023
@ottok
Copy link
Contributor

ottok commented Apr 9, 2023

@arun-esh Would you like to review this? It is somewhat similar to your #2573.

@robinnewhouse
Copy link
Contributor Author

Removed changes that were the cause of failing tests. Could this be reviewed?
cc @arun-esh

@robinnewhouse robinnewhouse force-pushed the 10.4-safe-strcpy branch 2 times, most recently from 7130ec6 to 2d73c86 Compare May 4, 2023 19:27
Copy link
Contributor

@LinuxJedi LinuxJedi left a comment

Choose a reason for hiding this comment

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

Some minor changes needed. I flagged a couple of the changes that go > 80 characters, but there are several more too.

@@ -201,13 +201,13 @@ void Json_writer::add_size(longlong val)
{
/* Values less than 16MB are specified in KB for precision */
len= my_snprintf(buf, sizeof(buf), "%lld", val/1024);
strcpy(buf + len, "Kb");
safe_strcpy(buf + len, sizeof(buf) - len, "Kb");
Copy link
Contributor

Choose a reason for hiding this comment

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

This one and the one below should probably all be wrapped up into the my_snprintf()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. let me know if this looks alright to you.

sql/rpl_parallel.cc Outdated Show resolved Hide resolved
sql/rpl_parallel.cc Outdated Show resolved Hide resolved
sql/semisync_master.cc Outdated Show resolved Hide resolved
sql/sql_connect.cc Show resolved Hide resolved
Copy link
Contributor

@LinuxJedi LinuxJedi left a comment

Choose a reason for hiding this comment

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

Looks good to me, I'll pass on to a second review.

strcpy(buf + len, "Kb");
len+= 2;
}
len= my_snprintf(buf, sizeof(buf), "%lldKb", val/1024);
Copy link
Contributor

Choose a reason for hiding this comment

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

Something for another time (not this pull request). Lower case 'b' is the wrong terminology here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted. Thanks for the correction.

Copy link
Member

@cvicentiu cvicentiu left a comment

Choose a reason for hiding this comment

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

Minor style comment. Looks good to me. OK to push after the change.

sql/hostname.cc Outdated
@@ -513,42 +513,48 @@ int ip_to_hostname(struct sockaddr_storage *ip_storage,

DBUG_EXECUTE_IF("getnameinfo_error_noname",
{
strcpy(hostname_buffer, "<garbage>");
safe_strcpy(hostname_buffer, sizeof(hostname_buffer),
"<garbage>");
Copy link
Member

Choose a reason for hiding this comment

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

Indentation is wrong here. The string should be aligned with the opening ( on the previous row. Same as the other DBUG_EXECUTE_IFs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks @cvicentiu, just to avoid pushing commits back and forth I'll just ask here.
Is this the indentation you had in mind?

  DBUG_EXECUTE_IF("getnameinfo_error_noname",
                  {
                    safe_strcpy(hostname_buffer, sizeof(hostname_buffer),
                                "<garbage>");
                    err_code= EAI_NONAME;
                  }
                  );

@robinnewhouse
Copy link
Contributor Author

Is this ok to push to 10.4 or would you like me to rebase to 10.5?

@vuvova
Copy link
Member

vuvova commented Apr 17, 2024

I'd suggest 10.5, just to be on the safe side. We're doing the very last 10.4 within a couple of weeks.

@robinnewhouse robinnewhouse changed the base branch from 10.4 to 10.5 April 17, 2024 19:36
@robinnewhouse
Copy link
Contributor Author

robinnewhouse commented Apr 17, 2024

@vuvova Rebased to 10.5
@cvicentiu Updated indentation.

Copy link
Member

@vuvova vuvova left a comment

Choose a reason for hiding this comment

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

approving, based on #2516 (review)

Similar to MariaDB#2480.
567b681 introduced safe_strcpy() to minimize the use of C with
potentially unsafe memory overflow with strcpy() whose use is
discouraged.
Replace instances of strcpy() with safe_strcpy() where possible, limited
here to files in the `sql/` directory.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer
Amazon Web Services, Inc.
@LinuxJedi LinuxJedi enabled auto-merge (rebase) May 17, 2024 11:56
@LinuxJedi LinuxJedi merged commit dc38d8e into MariaDB:10.5 May 17, 2024
12 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants