https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/120689
The %b printf extension in the kernel is not fixed to a int type. On sparc64 there are various %llb formats. Adjust the code to handle the length specifiers and type check like it is used by the regular case. >From a0b6bab87a50cd0c66d8c31ca2f16376de826955 Mon Sep 17 00:00:00 2001 From: Brad Smith <b...@comstyle.com> Date: Thu, 19 Dec 2024 21:35:57 -0500 Subject: [PATCH] [clang][Sema] Fixes for %b printf extension handling The %b printf extension in the kernel is not fixed to a int type. On sparc64 there are various %llb formats. Adjust the code to handle the length specifiers and type check like it is used by the regular case. --- clang/lib/Sema/SemaChecking.cpp | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index f6c4def2892554..93a09ac65b8cad 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -7191,18 +7191,33 @@ bool CheckPrintfHandler::HandlePrintfSpecifier( // Claim the second argument. CoveredArgs.set(argIndex + 1); - // Type check the first argument (int for %b, pointer for %D) const Expr *Ex = getDataArg(argIndex); - const analyze_printf::ArgType &AT = - (CS.getKind() == ConversionSpecifier::FreeBSDbArg) ? - ArgType(S.Context.IntTy) : ArgType::CPointerTy; - if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) - EmitFormatDiagnostic( + if (CS.getKind() == ConversionSpecifier::FreeBSDDArg) { + // Type check the first argument (pointer for %D) + const analyze_printf::ArgType &AT = ArgType::CPointerTy; + if (AT.isValid() && !AT.matchesType(S.Context, Ex->getType())) + EmitFormatDiagnostic( S.PDiag(diag::warn_format_conversion_argument_type_mismatch) - << AT.getRepresentativeTypeName(S.Context) << Ex->getType() - << false << Ex->getSourceRange(), - Ex->getBeginLoc(), /*IsStringLocation*/ false, + << AT.getRepresentativeTypeName(S.Context) << Ex->getType() + << false << Ex->getSourceRange(), + Ex->getBeginLoc(), /*IsStringLocation*/false, getSpecifierRange(startSpecifier, specifierLen)); + } else { + // Check the length modifier for %b + if (!FS.hasValidLengthModifier(S.getASTContext().getTargetInfo(), + S.getLangOpts())) + HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, + diag::warn_format_nonsensical_length); + else if (!FS.hasStandardLengthModifier()) + HandleNonStandardLengthModifier(FS, startSpecifier, specifierLen); + else if (!FS.hasStandardLengthConversionCombination()) + HandleInvalidLengthModifier(FS, CS, startSpecifier, specifierLen, + diag::warn_format_non_standard_conversion_spec); + + // Type check the first argument of %b + if (!checkFormatExpr(FS, startSpecifier, specifierLen, Ex)) + return false; + } // Type check the second argument (char * for both %b and %D) Ex = getDataArg(argIndex + 1); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits