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

Printf format checks are skipped for constexpr format strings #55805

Closed
paradust7 opened this issue Jun 1, 2022 · 3 comments
Closed

Printf format checks are skipped for constexpr format strings #55805

paradust7 opened this issue Jun 1, 2022 · 3 comments
Assignees
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party

Comments

@paradust7
Copy link

paradust7 commented Jun 1, 2022

demo.cpp:

#include <cstdio>

constexpr const char* foo() {
  return "%s %d";
}

struct Bar {
    static constexpr char value[] = {'%', 's', '%', 'd', '\0'};
};
constexpr char Bar::value[];

int main() {
   printf(foo(), "abc", "def");
   printf(Bar::value, "abc", "def");
   return 0;
}

Clang does not produce any warning. gcc 11.2.0 produces both warnings:

demo.cpp: In function ‘int main()’:
demo.cpp:13:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘const char*’ [-Wformat=]
demo.cpp:14:16: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘const char*’ [-Wformat=]

Clang version is:

clang version 15.0.0 (https://github.com/llvm/llvm-project.git a72cc958a386e5fc97e8c30137fb56eb77ef571c)
Target: x86_64-unknown-linux-gnu
Thread model: posix
@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Jun 1, 2022
@inclyc
Copy link
Member

inclyc commented Jul 25, 2022

clang thinks these expressions are NOT literal, statically evaluated expressions are not considered as literal currently, and it fallbacks to warn you -Wformat-nonliteral (returns before "real" format string checking)

int main() {
   printf(foo(), "abc", "def"); // CallExprClass
   printf(Bar::value, "abc", "def"); // DeclRefExprClass
   return 0;
}

Related to #56583

See: https://godbolt.org/z/oKf6733K4

@inclyc
Copy link
Member

inclyc commented Jul 25, 2022

May be helpful to see clang/lib/Sema/SemaChecking.cpp

// Determine if an expression is a string literal or constant string.
// If this function returns false on the arguments to a function expecting a
// format string, we will usually need to emit a warning.
// True string literals are then checked by CheckFormatString.
static StringLiteralCheckType
checkFormatStringExpr(Sema &S, const Expr *E, ArrayRef<const Expr *> Args,
                      Sema::FormatArgumentPassingKind APK, unsigned format_idx,
                      unsigned firstDataArg, Sema::FormatStringType Type,
                      Sema::VariadicCallType CallType, bool InFunctionCall,
                      llvm::SmallBitVector &CheckedVarArgs,
                      UncoveredArgHandler &UncoveredArg, llvm::APSInt Offset,
                      bool IgnoreStringsWithoutSpecifiers = false) {

/* statements */
}

@inclyc inclyc added the confirmed Verified by a second party label Jul 25, 2022
@inclyc inclyc self-assigned this Jul 26, 2022
@inclyc
Copy link
Member

inclyc commented Aug 1, 2022

inclyc added a commit that referenced this issue Aug 4, 2022
This patch enhances clang's ability to check compile-time determinable
string literals as format strings, and can give FixIt hints at literals
(unlike gcc). Issue #55805
mentiond two compile-time string cases. And this patch partially fixes
one.

```
constexpr const char* foo() {
  return "%s %d";
}
int main() {
   printf(foo(), "abc", "def");
   return 0;
}
```

This patch enables clang check format string for this:

```
<source>:4:24: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
  printf(foo(), "abc", "def");
         ~~~~~         ^~~~~
<source>:2:42: note: format string is defined here
constexpr const char *foo() { return "%s %d"; }
                                         ^~
                                         %s
1 warning generated.
```

Reviewed By: aaron.ballman

Signed-off-by: YingChi Long <me@inclyc.cn>

Differential Revision: https://reviews.llvm.org/D130906
@inclyc inclyc closed this as completed Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

3 participants