This is an archive of the discontinued LLVM Phabricator instance.

[clang][Sema] Add truncation warning on fortified snprintf
ClosedPublic

Authored by hazohelet on Aug 22 2023, 4:09 PM.

Details

Summary

This patch warns on snprintf calls whose n argument is known to be smaller than the size of the formatted string like

char buf[5];
snprintf(buf, 5, "Hello");

This is a counterpart of gcc's Wformat-truncation
Fixes https://github.com/llvm/llvm-project/issues/64871

Diff Detail

Event Timeline

hazohelet created this revision.Aug 22 2023, 4:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2023, 4:09 PM
hazohelet requested review of this revision.Aug 22 2023, 4:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2023, 4:09 PM

Can't say much about the rest of the patch so I'd defer to someone else. Since gcc has -Wformat-truncation, can we disable this warning in clang as well? Can you add a test for that?

clang/lib/Sema/SemaChecking.cpp
1156–1157
1348

(Really not a fan of this, it adds nothing).

Thanks for the patch!

clang/lib/Sema/SemaChecking.cpp
1154

naive question, so will we create a lambda with capture even though a few cases in the switch below will never use it?

1347

Can DestinationSize be calculated later, after the if (SourceSize && ...? Seems it's not needed unless that condition is false?

hazohelet updated this revision to Diff 552939.Aug 23 2023, 6:02 PM
hazohelet marked 4 inline comments as done.

Address review comments

Gcc can diagnose wider cases of overflow/truncation by specifying a higher level for it, like -Wformat-overflow=2 (https://godbolt.org/z/n5facjW1c).
The current clang counterpart only diagnoses when the format string is always larger than the buffer size. If we are going to implement more general warnings like gcc's, I think we should separate these counterparts into their own warning flags (-Wformat-overflow/truncation).

clang/lib/Sema/SemaChecking.cpp
1154

I made it a static function.

nickdesaulniers accepted this revision.Aug 24 2023, 10:52 AM
This revision is now accepted and ready to land.Aug 24 2023, 10:52 AM

Thank you for working on this!

Precommit CI found an issue with libc++'s tests: https://reviews.llvm.org/harbormaster/unit/view/8606248/ -- it'd be best if those could be addressed before landing the changes.

Thank you for working on this!

Precommit CI found an issue with libc++'s tests: https://reviews.llvm.org/harbormaster/unit/view/8606248/ -- it'd be best if those could be addressed before landing the changes.

Thanks for the heads up! I was missing that.
The reason for the false positive is that clang doesn't know that %g specifier can result in a print of only a single digit. (Live demo: https://godbolt.org/z/8aeqh1Wsf)

hazohelet updated this revision to Diff 553246.Aug 24 2023, 1:52 PM

Fixes EstimateSizeFormatHandler's %g handling.

aaron.ballman accepted this revision.Aug 25 2023, 5:49 AM

LGTM!

clang/lib/Sema/SemaChecking.cpp
1041