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

Use base::StringPrintf in constexrp contexts #25643

Merged
merged 1 commit into from
Sep 19, 2024

Conversation

cdesouza-chromium
Copy link
Collaborator

@cdesouza-chromium cdesouza-chromium commented Sep 19, 2024

Upstream has made this function constexpr, in order to force callers to change its call sites to use a constexpr string, in order to be able to validate at compile time the format string. There is an alternative base::StringPrintfNonConstexpr, but obviously this unsafe variant should be avoided whenever possible.

This change goes over the different places where the constexpr constrained was being violated, and corrects each callsite. This has also revealed a buggy test in SimulationResponseParserUnitTest, where the formatting string wasn't correct.

Chromium change:
https://chromium.googlesource.com/chromium/src/+/8bf39fbefcd2963b3647c24f2b27dfb5c40875c4

commit 8bf39fbefcd2963b3647c24f2b27dfb5c40875c4
Author: Peter Kasting <pkasting@chromium.org>
Date:   Fri Sep 13 19:07:33 2024 +0000

    Disallow StringPrintf() with non-constexpr format strings.

    Bug: 365705855

Resolves brave/brave-browser#41137

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

Copy link
Member

@yrliou yrliou left a comment

Choose a reason for hiding this comment

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

wallet-core++

Copy link
Member

@diracdeltas diracdeltas left a comment

Choose a reason for hiding this comment

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

i think sec team was tagged for the network audit whitelist change but @SergeyZhukovsky should take a look at it

@cdesouza-chromium cdesouza-chromium force-pushed the use-consexpr-string-printf branch 2 times, most recently from 32d4f43 to e61704e Compare September 19, 2024 03:19
Copy link
Contributor

@iefremov iefremov left a comment

Choose a reason for hiding this comment

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

browser/net

Upstream has made this function constexpr, in order to force callers to
change its call sites to use a constexpr string, in order to be able to
validate at compile time the format string. There is an alternative
`base::StringPrintfNonConstexpr`, but obviously this unsafe variant
should be avoided whenever possible.

This change goes over the different places where the `constexpr`
constrained was being violated, and corrects each callsite. This has
also revealed a buggy test in `SimulationResponseParserUnitTest`, where
the formatting string wasn't correct.

Chromium change:
https://chromium.googlesource.com/chromium/src/+/8bf39fbefcd2963b3647c24f2b27dfb5c40875c4

commit 8bf39fbefcd2963b3647c24f2b27dfb5c40875c4
Author: Peter Kasting <pkasting@chromium.org>
Date:   Fri Sep 13 19:07:33 2024 +0000

    Disallow StringPrintf() with non-constexpr format strings.

    Bug: 365705855
Copy link
Contributor

[puLL-Merge] - brave/brave-core@25643

Description

This PR makes several changes to improve code quality and consistency across multiple components of the Brave browser. The main changes involve replacing string formatting methods, removing unused code, and refactoring some functions for better readability and maintainability.

Changes

Changes

  1. browser/net/brave_network_audit_allowed_lists.h:

    • Removed kAllowedBraveSearchTemplates array.
  2. browser/net/brave_network_audit_search_ad_browsertest.cc:

    • Replaced base::StringPrintf with base::StrCat for string concatenation.
    • Moved the Brave search template string into the function where it's used.
  3. components/brave_rewards/core/endpoints/brave/*.cc and *.h files:

    • Replaced base::StringPrintf with base::StrCat for string concatenation in multiple files.
    • Removed Path() methods and replaced them with constexpr strings or inline functions.
    • Updated function signatures to use base::cstring_view instead of const char*.
  4. components/brave_wallet/browser/*.cc files:

    • Replaced string literals defined in functions with constexpr variables.
    • Updated some string formatting calls to use base::StringPrintf more efficiently.
  5. components/brave_wallet/common/eth_request_helper_unittest.cc:

    • Replaced a string literal with a constexpr variable.

Overall, these changes aim to improve code readability, reduce string allocations, and make better use of C++ language features.

Copy link
Contributor

@emerick emerick left a comment

Choose a reason for hiding this comment

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

++Rewards

Copy link
Member

@SergeyZhukovsky SergeyZhukovsky left a comment

Choose a reason for hiding this comment

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

++

@cdesouza-chromium cdesouza-chromium merged commit 603c3aa into master Sep 19, 2024
17 checks passed
@cdesouza-chromium cdesouza-chromium deleted the use-consexpr-string-printf branch September 19, 2024 15:41
@github-actions github-actions bot added this to the 1.72.x - Nightly milestone Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use base::StringPrintf only with constexpr formats
7 participants