<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:arial,sans-serif">Hi!</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">I had the idea of a checker that inspects if the return value of specific system functions (or any other) is correctly checked for error. It is applicable to functions that return some value on success and a special "error return" value on failure, for example 'malloc' or 'rename'. The checker should verify that there is a part in the source code that tests if the returned value is the "error return" value, as the first "usage" of the returned value. There is a similar CERT rule ERR33-C (<a href="https://wiki.sei.cmu.edu/confluence/display/c/ERR33-C.+Detect+and+handle+standard+library+errors" target="_blank">https://wiki.sei.cmu.edu/confluence/display/c/ERR33-C.+Detect+and+handle+standard+library+errors</a>).<br></div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">My question is, does it make sense to have this kind of check, how can it be improved, is there other better way of implementing a similar mechanism. It seems that the algorithm is not easy to understand so I try to explain it here.</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">The patch D72705 contains the checker. It works the following way:<br></div><br>1. Detect a call to a (system) function that should be checked.<br>2. Returned value from the function is remembered.<br>3. The first place is found in the code where the returned value is "used" (read, loaded). The value can be assigned to other variables or passed into functions (or returned?) before this point.<br><br>void *M = malloc(10);<br>void *A;<br>A = M; // not an "use"<br>void *B = A; // not an "use"<br>f(M); // not an "use"<br><br>4. At the first "use" (that is found in step 3) it is checked if that use is valid. An "use" is an (clang AST node) expression where the value appears. Specific expressions are detected as "error check" by the checker, others as "invalid use". At the "error check" case the checker forgets the return value and does not produce warning. At the "invalid use" case a warning is produced. If neither are found, the next subexpression is evaluated (in the same expression tree or at later statements). This is a syntactical check of the expressions.<br><br>if (M == nullptr) // "error check" found<br>if (M == nullptr || <other things>) // "error check" found<br>if (nullptr == (M = malloc(10))) // "error check found"<br>void *M1 = (char*)M + 1 // "invalid use"<br>free(M) // "invalid use" ('free' is a system-call function)<br><br>If no "use" is found and the value is garbage-collected, a "unchecked value" can be reported by the checker.<br><br>I know that there are other ways of detecting similar bugs, but this can find cases that others don't. And no state split is done by this checker. The check can work for functions that return nullptr on error, or return other numerical constant value on error (that is used only to indicate if there was error). In the most simple case, if any read of the return value is found as "error check", it is a form of unused value check.</div><div dir="ltr"><br></div><div dir="ltr"><div class="gmail_default" style="font-family:arial,sans-serif">Thanks,</div></div><div dir="ltr"><div class="gmail_default" style="font-family:arial,sans-serif">Balázs</div><br></div></div></div></div></div></div></div></div>