Bug 80806 - gcc does not warn if local array is memset only
Summary: gcc does not warn if local array is memset only
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 5.4.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, patch
Depends on:
Blocks:
 
Reported: 2017-05-17 15:47 UTC by Daniel Fruzynski
Modified: 2023-07-07 07:59 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-05-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Fruzynski 2017-05-17 15:47:52 UTC
gcc has warning "variable ‘c’ set but not used". However this warning is not printed when local variable is cleared using memset, and then not used. Attempt to compile following code using gcc 5.4.0 prints warning regarding c var, but not regarding buf var:

#include <string.h>

void test()
{
    char buf[10];
    memset(buf, 0, sizeof(buf));

    int c;
    c = 1;
}

$ gcc -c -O3 -Wall -Wextra test.c -o test.o
test.c: In function ‘test’:
test.c:8:9: warning: variable ‘c’ set but not used [-Wunused-but-set-variable]
     int c;
         ^
Comment 1 Richard Biener 2017-05-18 08:00:48 UTC
Confirmed.
Comment 2 Martin Sebor 2017-05-18 19:58:42 UTC
The challenge is that the DECL_READ_P flag is set indiscriminately by the front end (in convert_lvalue_to_rvalue called from c_parser_expr_list while parsing the argument list in the function call expression) in response to seeing the array being passed to a function as an argument.  There's no logic that would take into account the properties of the function is being called.  In general, GCC also has no notion of "out" only arguments (such as memset's first), or "in" only (such as memcpy's second).  It may be a useful concept to introduce in the form of an attribute.  With such an attribute, this issue could then be detected in for any function whose arguments were decorated with it.
Comment 3 Daniel Fruzynski 2017-05-19 09:22:05 UTC
"in" attribute is similar to "const", I am not sure if we need another one for this.

"out" attribute would be handy. I recall than in the past I was looking for it. gcc can print warning that uninitialized variable is passed to function. Now one has to initialize that variable before passing it to function. With this attribute initialization would not be necessary. Beside silenced warning this also would produce a bit faster executable.

BTW, C# provides "out" parameter modifier, but no "in".
Comment 4 Martin Sebor 2017-05-19 15:25:45 UTC
Right, const could be used in lieu of an "in" attribute for warnings.  It can't be used for the same thing for optimization because constness can be cast away.  Bug 10138 has a good discussion on the subject.

Let me see if I can prototype something.
Comment 5 prathamesh3492 2017-05-19 20:15:28 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2017-05/msg01493.html
Comment 6 Daniel Fruzynski 2017-05-20 09:35:37 UTC
I have checked list of my issues reported here and found Bug 68034 which is closely related to this one. This patch probably will fix that one too, user will know that his memset was removed by compiler.
Comment 7 Martin Sebor 2017-08-11 20:46:28 UTC
My patch for this warning causes regressions in Wunused-var-8.c added for bug 44108.  As it turns out, GCC considers it a use of a variable even when it's just mentioned in an unevaluated context (such as the sizeof expression in the call to memset in comment #0), without being evaluated.
Comment 8 Martin Sebor 2019-09-29 19:54:40 UTC
Patch: https://gcc.gnu.org/ml/gcc-patches/2019-09/msg01690.html
Comment 9 Jakub Jelinek 2020-05-07 11:56:12 UTC
GCC 10.1 has been released.
Comment 10 Richard Biener 2020-07-23 06:52:03 UTC
GCC 10.2 is released, adjusting target milestone.
Comment 11 Richard Biener 2021-04-08 12:02:23 UTC
GCC 10.3 is being released, retargeting bugs to GCC 10.4.
Comment 12 Martin Sebor 2022-01-26 17:11:00 UTC
I'm not working on this aspect anymore (though all the pieces are in place to enable the warning).
Comment 13 Jakub Jelinek 2022-06-28 10:33:23 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.