<div dir="ltr"><div><div><div>Hello,<br><br></div>I'm trying to write a clang checker to detect when the number of calls to va_arg exceeds the number of variadic arguments passed to a function. An example bug I'd like to be able to detect is shown at the end of this e-mail.<br><br></div><div>To do this, my plan is to register a map with the program state and, when va_start or va_copy is called, create an entry describing the number of arguments and our position within them. Then calls to va_args can be checked to ensure they're in that range.<br></div><div><br></div><div>Currently, I'm able to detect calls to va_start and va_args, so I'll be able to track the position within the argument list - but I'm not sure how to go about getting the length of the list of arguments.<br><br></div><div>Can anyone advise me on how I should find the number of va_args arguments?<br><br></div><div>Thanks.<br><br></div><div></div><div><br></div><div>Here's the progress I've made so far - as you can see, it looks a lot like the "Building a Checker in 24 hours" checker: <a href="https://github.com/michaeltandy/clang/blob/db5c117d3cdc5287829dd4e55166aeb2ef1ff08a/lib/StaticAnalyzer/Checkers/VariadicChecker.cpp">https://github.com/michaeltandy/clang/blob/db5c117d3cdc5287829dd4e55166aeb2ef1ff08a/lib/StaticAnalyzer/Checkers/VariadicChecker.cpp</a><br></div><div><br><br></div>Example bug code:<br><br><span style="font-family:monospace,monospace">#include <stdarg.h><br>#include <stdio.h><br><br>static void printArgs(int x, ...) {<br>    va_list args;<br>    va_start( args, x );<br>    for (int i=0 ; i<x; i++) {<br>        printf("%i ", va_arg(args, int));<br>    }<br>    printf("\n");<br>    va_end( args );<br>}<br><br>static void printArgs2(int x, ...) {<br>    printArgs(x);<br>}<br><br>int main () {<br>    printArgs(4, 1, 2, 3, 4);<br>    printArgs2(4, 1, 2, 3, 4);<br>    return(0);<br>}<br><br></span></div><span style="font-family:arial,helvetica,sans-serif">Example bug consequences:</span><span style="font-family:monospace,monospace"><br><br>$ ./bin/clang ../stdarg-example2.c <br>$ ./a.out <br>1 2 3 4 <br>1 2 0 4 <br><br></span></div>