<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{mso-style-priority:34;
margin-top:0in;
margin-right:0in;
margin-bottom:0in;
margin-left:.5in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
@list l0
{mso-list-id:236286216;
mso-list-type:hybrid;
mso-list-template-ids:829425366 67698689 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l0:level2
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l0:level3
{mso-level-number-format:bullet;
mso-level-text:\F0A7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l0:level4
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l0:level5
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l0:level6
{mso-level-number-format:bullet;
mso-level-text:\F0A7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l0:level7
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Symbol;}
@list l0:level8
{mso-level-number-format:bullet;
mso-level-text:o;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:"Courier New";}
@list l0:level9
{mso-level-number-format:bullet;
mso-level-text:\F0A7 ;
mso-level-tab-stop:none;
mso-level-number-position:left;
text-indent:-.25in;
font-family:Wingdings;}
@list l1
{mso-list-id:1374111167;
mso-list-template-ids:-383467538;}
@list l1:level1
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level2
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:1.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level3
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:1.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level4
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:2.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level5
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:2.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level6
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:3.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level7
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:3.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level8
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:4.0in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
@list l1:level9
{mso-level-number-format:bullet;
mso-level-text:\F0B7 ;
mso-level-tab-stop:4.5in;
mso-level-number-position:left;
text-indent:-.25in;
mso-ansi-font-size:10.0pt;
font-family:Symbol;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hello,<o:p></o:p></p>
<p class="MsoNormal">I am interested in learning about clang’s -Wsometimes-uninitialized implementation and how is it different than gcc’s -Wmaybe-uninitialized. For example, if we consider this code:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">#include <stdlib.h><o:p></o:p></p>
<p class="MsoNormal">int test(int random, int cond1, int cond2) {<o:p></o:p></p>
<p class="MsoNormal"> volatile int *ptr;<o:p></o:p></p>
<p class="MsoNormal"> if (cond1) { //<<<< cond1<o:p></o:p></p>
<p class="MsoNormal"> ptr = (int *)calloc(1, sizeof(int));<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"> if (cond2) { //<<<< cond2 , independent of cond1<o:p></o:p></p>
<p class="MsoNormal"> *ptr = 1;<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"> return 0;<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">With gcc 8.3.1, I get the following output:<o:p></o:p></p>
<p class="MsoNormal"><b>$</b> gcc -g -c func.c -O1 -Wuninitialized<o:p></o:p></p>
<p class="MsoNormal">func.c: In function ‘test’:<o:p></o:p></p>
<p class="MsoNormal">func.c:8:10: warning: ‘ptr’ may be used uninitialized in this function [-Wmaybe-uninitialized]<o:p></o:p></p>
<p class="MsoNormal"> *ptr = 1;<o:p></o:p></p>
<p class="MsoNormal">(debug-llvm-11) /nobackup/alissidd/CSCvv46785$ vim func.c<o:p></o:p></p>
<p class="MsoNormal">(debug-llvm-11) /nobackup/alissidd/CSCvv46785$ gcc -O1 -Wuninitialized -Wmaybe-uninitialized -c func.c<o:p></o:p></p>
<p class="MsoNormal">func.c: In function ‘test’:<o:p></o:p></p>
<p class="MsoNormal">func.c:8:10: warning: ‘ptr’ may be used uninitialized in this function [-Wmaybe-uninitialized]<o:p></o:p></p>
<p class="MsoNormal"> *ptr = 1;<o:p></o:p></p>
<p class="MsoNormal"> ~~~~~^~~<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">However, with clang 11.0.0 I don’t get any warnings:<o:p></o:p></p>
<p class="MsoNormal"><b>$</b> clang -O1 -Wuninitialized -Wsometimes-uninitialized -c func.c<o:p></o:p></p>
<p class="MsoNormal"><b>$<o:p></o:p></b></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">It was detected by the static analyzer though:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Scan-build clang -O1 -Wuninitialized -Wsometimes-uninitialized -c func.c<o:p></o:p></p>
<p class="MsoNormal">scan-build: Using 'clang-11' for static analysis<o:p></o:p></p>
<p class="MsoNormal">func.c:8:10: warning: Dereference of undefined pointer value [core.NullDereference]<o:p></o:p></p>
<p class="MsoNormal"> *ptr = 1;<o:p></o:p></p>
<p class="MsoNormal"> ~~~~~^~~<o:p></o:p></p>
<p class="MsoNormal">func.c:10:10: warning: Potential leak of memory pointed to by 'ptr' [unix.Malloc]<o:p></o:p></p>
<p class="MsoNormal"> return 0;<o:p></o:p></p>
<p class="MsoNormal"> ^<o:p></o:p></p>
<p class="MsoNormal">2 warnings generated.<o:p></o:p></p>
<p class="MsoNormal">scan-build: Analysis run complete.<o:p></o:p></p>
<p class="MsoNormal">scan-build: 2 bugs found.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I did see the -Wsometimes-uninitialized flag working, if for example, I move the use of the
<b>ptr</b> variable outside of a condition:<o:p></o:p></p>
<p class="MsoNormal">#include <stdlib.h><o:p></o:p></p>
<p class="MsoNormal">int test(int random, int cond1, int cond2) {<o:p></o:p></p>
<p class="MsoNormal"> volatile int *ptr;<o:p></o:p></p>
<p class="MsoNormal"> if (cond1) { //<<<< cond1<o:p></o:p></p>
<p class="MsoNormal"> ptr = (int *)calloc(1, sizeof(int));<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"> if (cond2) { //<<<< cond2 , independent of cond1<o:p></o:p></p>
<p class="MsoNormal"> *ptr = 1;<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"> <b>return ptr</b>;<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><b>$</b> clang -O1 -Wuninitialized -Wsometimes-uninitialized -c func.c<o:p></o:p></p>
<p class="MsoNormal">func.c:10:10: warning: incompatible pointer to integer conversion returning 'volatile int *' from a function with result type 'int' [-Wint-conversion]<o:p></o:p></p>
<p class="MsoNormal"> return ptr;<o:p></o:p></p>
<p class="MsoNormal"> ^~~<o:p></o:p></p>
<p class="MsoNormal">func.c:4:7: warning: variable 'ptr' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]<o:p></o:p></p>
<p class="MsoNormal"> if (cond1) { //<<<< cond1<o:p></o:p></p>
<p class="MsoNormal"> ^~~~~<o:p></o:p></p>
<p class="MsoNormal">func.c:10:10: note: uninitialized use occurs here<o:p></o:p></p>
<p class="MsoNormal"> return ptr;<o:p></o:p></p>
<p class="MsoNormal"> ^~~<o:p></o:p></p>
<p class="MsoNormal">func.c:4:3: note: remove the 'if' if its condition is always true<o:p></o:p></p>
<p class="MsoNormal"> if (cond1) { //<<<< cond1<o:p></o:p></p>
<p class="MsoNormal"> ^~~~~~~~~~~<o:p></o:p></p>
<p class="MsoNormal">func.c:3:20: note: initialize the variable 'ptr' to silence this warning<o:p></o:p></p>
<p class="MsoNormal"> volatile int *ptr;<o:p></o:p></p>
<p class="MsoNormal"> ^<o:p></o:p></p>
<p class="MsoNormal"> = NULL<o:p></o:p></p>
<p class="MsoNormal">2 warnings generated.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m new to clang development and would appreciate the help with the following questions:<o:p></o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">Are clang’s -Wsometimes-uninitialized and gcc’s -Wmaybe-unintialized comparable?<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">As is shown that a use of an uninitialized variable within a condition could not be detected by the -Wuninitialized/-Wsometimes-uninitialized flag, is this behavior by design?<o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1">I’m trying to understand the source code of the -Wuninitialized and -Wsometimes-initialized in particular and I was able to single out the files that maybe used to implement it:<o:p></o:p></li><ul style="margin-top:0in" type="circle">
<li class="MsoNormal" style="mso-list:l0 level2 lfo1;vertical-align:middle">clang/lib/Analysis/UninitializedValues.cpp<o:p></o:p></li><li class="MsoNormal" style="mso-list:l0 level2 lfo1;vertical-align:middle">clang/include/clang/Analysis/Analyses/UninitializedValues.h<o:p></o:p></li><li class="MsoNormal" style="mso-list:l0 level2 lfo1;vertical-align:middle">clang/lib/Sema/AnalysisBasedWarnings.cpp<o:p></o:p></li></ul>
</ul>
<p class="MsoNormal" style="margin-left:.5in">Any pointers about how should I go about reading and understanding it will be very helpful.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thank you,<o:p></o:p></p>
<p class="MsoNormal">Ali<o:p></o:p></p>
</div>
</body>
</html>