[cfe-dev] clang and clang-sa do not detect uninitialized variable

Deep Majumder via cfe-dev cfe-dev at lists.llvm.org
Tue May 4 07:48:39 PDT 2021


Hi Ali,
By clang-sa if you mean the Clang Static Analyser, then you need to add a
checker for that. You may want to look up the "Static Analyser Checker in
24 hrs talk"  (1) and also Artem Dergachev's LLVM conf talk (2). Also the
CSA book will also probably help (3).
If you do make the checker, then we would love to have it as a patch 😀.
Warm regards,
Deep

1) https://m.youtube.com/watch?v=kdxlsP5QVPw&t=1956s
2) https://m.youtube.com/watch?v=g0Mqx1niUi0&t=1545s
3)
https://www.youtube.com/redirect?event=comments&redir_token=QUFFLUhqbFU4TThJZk9jRktXYUJkWHRlTFUtclUzUE1Yd3xBQ3Jtc0traDFjNnBuTlhaQ1lpaV9TV3pIRjFVUzN1MkRtRjVKY3RYNkdZWHREcU10emFkNEJHcThVWlVIZWNxT2NEcVh3RU1CVFo4Uy1mbWFIZ1NUazZnN1lzSkFjSlI0OG1SWWNTT3FJMEpEQnRyZnBoOXNabw&q=https%3A%2F%2Fgithub.com%2FhaoNoQ%2Fclang-analyzer-guide%2Freleases%2Fdownload%2Fv0.1%2Fclang-analyzer-guide-v0.1.pdf

On Tue, 4 May, 2021, 8:05 pm Nathan Sidwell via cfe-dev, <
cfe-dev at lists.llvm.org> wrote:

> On 5/4/21 10:30 AM, Ali Shuja Siddiqui (alissidd) wrote:
> > Hi Nathan,
> >
> > Thanks for the reply. Can you please elaborate on how to let clang-sa to
> > learn about printf? I tried using CTU analysis [1], but I’m unsure how
> > to add mappings for libc functions.
>
> Sorry, I don't know.  I don't know if clang/llvm have support for
> knowing about various builtin functions the standard library provides.
> It must have some (like say abs and sqrt).  If it knew about printf, it
> could provide diagnostics when the variadic args do not match the format
> string, for instance.
>
> nathan
>
> > Thanks,
> >
> > Ali
> >
> > [1]
> https://clang.llvm.org/docs/analyzer/user-docs/CrossTranslationUnit.html
> >
> > *From: *Nathan Sidwell <nathanmsidwell at gmail.com> on behalf of Nathan
> > Sidwell <nathan at acm.org>
> > *Date: *Friday, April 30, 2021 at 9:00 AM
> > *To: *Ali Shuja Siddiqui (alissidd) <alissidd at cisco.com>,
> > cfe-dev at lists.llvm.org <cfe-dev at lists.llvm.org>
> > *Subject: *Re: [cfe-dev] clang and clang-sa do not detect uninitialized
> > variable
> >
> > On 4/29/21 4:41 PM, Ali Shuja Siddiqui (alissidd) via cfe-dev wrote:
> >  > Hello,
> >  >
> >  > With the following code
> >  >
> >  > //-------------------------
> >  >
> >  > #include <stdio.h>
> >  >
> >  > extern int t;
> >  >
> >  > void use_b (int *b){
> >  >
> >  >      printf("%p\n",b);
> >  >
> >  > }
> >  >
> >  > void func(){
> >  >
> >  >      int b;
> >  >
> >  >      use_b(&b);
> >  >
> >  >      if (b)
> >  >
> >  >          b+=33;
> >  >
> >  > }
> >  >
> >  > //---------------------------
> >  >
> >  > Running clang -Wuninitialized or running clang –analyze, I don’t see
> any
> >  > warning for uninitialized variables. However, if I change the code to:
> >  >
> >  > //----------------------
> >  >
> >  > #include <stdio.h>
> >  >
> >  > extern int t;
> >  >
> >  > void use_b (int *b){
> >  >
> >  >      if (t==5)
> >  >
> >  >          return;
> >  >
> >  >      printf("%p\n",b);
> >  >
> >  > }
> >  >
> >  > void func(){
> >  >
> >  >      int b;
> >  >
> >  >      use_b(&b);
> >  >
> >  >      if (b)
> >  >
> >  >          b+=33;
> >  >
> >  > }
> >  >
> >  > //--------------------
> >  >
> >  > I do see this warning with the static analyzer:
> >  >
> >  > sa_try.c:13:9: warning: Branch condition evaluates to a garbage value
> >  > [core.uninitialized.Branch]
> >  >
> >  >      if (b)
> >  >
> >  >          ^
> >  >
> >  > 1 warning generated.
> >  >
> >  > My question is why am I not getting any warning for the first case? Is
> >  > it being considered that printf is updating the value of b in some
> way?
> >
> > Presuming it has no knowledge of printf's semantics, then yes.  that
> > function could write through the pointer (and indeed would, if 'b' was
> > an output parameter).
> >
> > It could learn that printf doesn't do that (except for %n).
> >
> > nathan
> >
> >
> > nathan
> > --
> > Nathan Sidwell
> >
>
>
> --
> Nathan Sidwell
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210504/7912bb79/attachment.html>


More information about the cfe-dev mailing list