Skip to content

Conversation

@laanwj
Copy link
Member

@laanwj laanwj commented Oct 28, 2019

Our formatter, tinyformat, never needs c_str() for strings. Still, many places call it redundantly, resulting in longer code and a slight overhead.

Remove redundant c_str() calls for:

  • strprintf
  • LogPrintf
  • tfm::format

(also, combined with #17095, I think this improves logging in case of unexpected embedded NULL characters)

Our formatter, tinyformat, *never* needs `c_str()` for strings.
Remove redundant `c_str()` calls for:

- `strprintf`
- `LogPrintf`
- `tfm::format`
@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 28, 2019

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #16545 (Implement missing error checking for ArgsManager flags by ryanofsky)
  • #16224 (gui: Bilingual GUI error messages by hebasto)
  • #15934 (Merge settings one place instead of five places by ryanofsky)
  • #14866 (Improve property evaluation way in bitcoin.conf by AkioNak)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@fanquake
Copy link
Member

Looks like there is still a usage of c_str() with LogPrintf in wallet.h:

LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);

@laanwj
Copy link
Member Author

laanwj commented Oct 28, 2019

That one is special: it adds a prefix to the format string, which is a c_str(). It's something not normally allowed, and we even have an exception in the linter for it 😄

@practicalswift
Copy link
Contributor

Concept ACK: nice cleanup

@ryanofsky
Copy link
Contributor

Code review ACK c72906d. Easy to review with git log -p -n1 --word-diff-regex=. -U0 c72906dcc11a73fa06a0adf97557fa756b551bee

@maflcko
Copy link
Member

maflcko commented Oct 28, 2019

ACK, the args are:

std::string format(const char* formatString, const Args&... args);

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request Oct 28, 2019
…atting

c72906d refactor: Remove redundant c_str() calls in formatting (Wladimir J. van der Laan)

Pull request description:

  Our formatter, tinyformat, *never* needs `c_str()` for strings. Still, many places call it redundantly, resulting in longer code and a slight overhead.

  Remove redundant `c_str()` calls for:

  - `strprintf`
  - `LogPrintf`
  - `tfm::format`

  (also, combined with bitcoin#17095, I think this improves logging in case of unexpected embedded NULL characters)

ACKs for top commit:
  ryanofsky:
    Code review ACK c72906d. Easy to review with `git log -p -n1 --word-diff-regex=. -U0 c72906d`

Tree-SHA512: 9e21e7bed8aaff59b8b8aa11571396ddc265fb29608c2545b1fcdbbb36d65b37eb361db6688dd36035eab0c110f8de255375cfda50df3d9d7708bc092f67fefc
@maflcko maflcko merged commit c72906d into bitcoin:master Oct 28, 2019
@maflcko
Copy link
Member

maflcko commented Oct 28, 2019

Can we make it a compile time error to pass c-strings in to format? Passing them in should never be necessary.

@maflcko
Copy link
Member

maflcko commented Oct 28, 2019

Can we make it a compile time error to pass c-strings in to format? Passing them in should never be necessary.

Oh, I guess that is not true. One might want to print "%s" verbose, like this:

tfm::format(std::cerr, "%s", "%s");

@laanwj
Copy link
Member Author

laanwj commented Oct 28, 2019

Can we make it a compile time error to pass c-strings in to format? Passing them in should never be necessary.

I don't think this is possible, as a function that only takes std::string will accept C string due to the implicit constructor.

deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Apr 30, 2020
Summary:
Our formatter, tinyformat, *never* needs `c_str()` for strings.
Remove redundant `c_str()` calls for:

- `strprintf`
- `LogPrintf`
- `tfm::format`

This is a backport of Core [[bitcoin/bitcoin#17279 | PR17279]]

Test Plan:
  ninja all check

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D5908
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants