Bug 96725 - warn for uses of global nonconstant unterminated char arrays where strings are required
Summary: warn for uses of global nonconstant unterminated char arrays where strings ar...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 11.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wstringop-overread
  Show dependency treegraph
 
Reported: 2020-08-20 16:59 UTC by Martin Sebor
Modified: 2021-01-21 22:46 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-01-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-08-20 16:59:16 UTC
Although using a non-constant array initialized to all non-nul characters where a nul-terminated string is expected doesn't necessarily imply the initializer is  what's used, it is an indication that it might be used, and so a warning would be helpful, especially in code that doesn't make use of const.

$ cat z.c && gcc -O2 -S -Wall -Wrestrict -fdump-tree-strlen=/dev/stdout z.c
char a[4] = { 1, 2, 3, 4 };

int f (void) { return __builtin_strlen (a); }   // warn here

;; Function f (f, funcdef_no=0, decl_uid=1932, cgraph_uid=1, symbol_order=1)

;; 1 loops found
;;
;; Loop 0
;;  header 0, latch 1
;;  depth 0, outer -1
;;  nodes: 0 1 2
;; 2 succs { 1 }
f ()
{
  long unsigned int _1;
  int _3;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_strlen (&a);
  _3 = (int) _1;
  return _3;

}
Comment 1 Martin Sebor 2021-01-21 22:46:45 UTC
Confirming as a -Wstringop-overread enhancement.