This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add check for passing the result of `std::string::c_str` to `strlen`
Needs RevisionPublic

Authored by acoster on Jan 4 2023, 4:41 AM.

Details

Summary

Suggests replacing the call with std::string::size.

Diff Detail

Event Timeline

acoster created this revision.Jan 4 2023, 4:41 AM
Herald added a project: Restricted Project. · View Herald Transcript
acoster requested review of this revision.Jan 4 2023, 4:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 4:41 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Eugene.Zelenko added inline comments.
clang-tools-extra/clang-tidy/readability/StrlenStringCStrCheck.cpp
2

Please make it single string.

36

What about std::basic_string_view? Or any class with c_str/data/length/size?

66

Please fix.

clang-tools-extra/docs/ReleaseNotes.rst
135

Please use double back-ticks for language constructs.

clang-tools-extra/docs/clang-tidy/checks/readability/strlen-string-cstr.rst
7

Please make it same as statement in Release Notes.

23

Please fix.

clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp
118

Please fix.

carlosgalvezp added inline comments.Jan 4 2023, 7:32 AM
clang-tools-extra/clang-tidy/readability/StrlenStringCStrCheck.h
24

The docs say this is "true" by default, but here you are setting it to false.

clang-tools-extra/docs/clang-tidy/checks/readability/strlen-string-cstr.rst
25

Is there a use case for wanting this option? (As opposed to unconditionally warning about data()) The problem is the same and the same fix applies?

carlosgalvezp added inline comments.Jan 4 2023, 7:33 AM
clang-tools-extra/test/clang-tidy/checkers/readability/strlen-string-cstr.cpp
2

The default case typically doesn't need a suffix.

3

Please give a more descriptive name

36

Tip: you don't need to write duplicate checks, you can simply skip the suffix and it will apply to both RUN lines:

CHECK-MESSAGES: ...
CHECK-FIXES: ...

acoster updated this revision to Diff 486504.Jan 5 2023, 2:29 AM

Generlised the check to cover string_view and "string-like" classes

Removed the option of ignoring data(), and generalised the check to suggest
fixes if the result of data() or c_str() method of any class with a public
length() or size() method is passed to strlen.

acoster marked 12 inline comments as done.Jan 5 2023, 2:33 AM
acoster added inline comments.
clang-tools-extra/docs/clang-tidy/checks/readability/strlen-string-cstr.rst
25

@carlosgalvezp Removed the option.

acoster marked an inline comment as done.Jan 5 2023, 2:35 AM
Eugene.Zelenko added inline comments.Jan 5 2023, 6:45 AM
clang-tools-extra/clang-tidy/readability/StrlenStringCStrCheck.h
2

Should be 80 characters long. See other checks as example.

acoster updated this revision to Diff 486582.Jan 5 2023, 7:46 AM

Fix length of comment at the top of StrlenStringCStrCheck.h

acoster marked an inline comment as done.Jan 5 2023, 7:48 AM
njames93 requested changes to this revision.Jan 5 2023, 9:07 AM

I don't see the appear of this check as its a situation that I doubt ever appears in code bases. If there are open source code bases where this is a known problem can you please provide links to them as well as running the run_clang_tidy script over them to verify the changes are good.

This revision now requires changes to proceed.Jan 5 2023, 9:07 AM

I don't see the appear of this check as its a situation that I doubt ever appears in code bases. If there are open source code bases where this is a known problem can you please provide links to them as well as running the run_clang_tidy script over them to verify the changes are good.

After a look at some projects, it seems like the pattern does arise, but often it's intended (eg a string buffer is set to a large size, a legacy function taking a char* writes to it, and then the string is resized to strlen(foo.c_str())). As such, I'll drop this patch, as it can introduce bugs in these cases.

LegalizeAdulthood resigned from this revision.Mar 29 2023, 8:07 AM