[Bug c/87367] New: GCC gives false warning on -Wnull-dereference when using -O2
mytbk920423 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Sep 20 08:11:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87367
Bug ID: 87367
Summary: GCC gives false warning on -Wnull-dereference when
using -O2
Product: gcc
Version: 8.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: mytbk920423 at gmail dot com
Target Milestone: ---
When compiling the following program with -Wnull-dereference, GCC doesn't warn
when using -O1, but warns when using -O2.
#include <stdlib.h>
typedef struct list_element_s {
void *data;
struct list_element_s *next;
} list_element_s, list_element_p[1];
list_element_s *list_new() {
list_element_s *n = malloc(sizeof(list_element_s));
if (!n) {
return NULL;
}
n->data = NULL;
n->next = NULL;
return n;
}
size_t otrng_list_len(list_element_s *head) {
list_element_s *cursor = head;
size_t size = 0;
while (cursor) {
if (cursor->data) {
size++;
}
cursor = cursor->next;
}
return size;
}
list_element_s *otrng_list_copy(list_element_s *head) {
if (otrng_list_len(head) == 0) {
return NULL;
}
list_element_s *cursor = head;
list_element_s *copy = list_new();
if (!copy) {
return NULL;
}
copy->data = cursor->data;
copy->next = NULL;
list_element_s *ret = copy;
cursor = cursor->next;
while (cursor) {
copy->next = list_new();
copy = copy->next;
copy->data = cursor->data;
copy->next = NULL;
cursor = cursor->next;
}
return ret;
}
$ gcc -c -O2 -Wnull-dereference list.c
list.c: In function 'otrng_list_copy':
list.c:54:16: warning: potential null pointer dereference [-Wnull-dereference]
copy->next = NULL;
^
list.c:53:16: warning: potential null pointer dereference [-Wnull-dereference]
copy->data = cursor->data;
~~~~~~~~~~~^~~~~~~~~~~~~~
More information about the Gcc-bugs
mailing list