Additional gcc flags #446

Closed
opened 2021-07-19 15:31:54 +02:00 by gaisser · 3 comments
Owner

There are some additional warning flags that are not part of -Wall and -Wextra, which might add some useful checks. Some examples:

  • -Wduplicated-cond
    Warn about duplicated condition in if-else-if chains, such as
int foo(int a)
{
  int b;
  if (a == 0)
    b = 42;
  else if (a == 0)
    b = 43;
  return b;
}
  • -Wlogical-op
    Warn about use of logical operations where a bitwise operation probably was intended, such as
int foo(int a)
{
  a = a || 0xf0;
  return a;
}

It also warns when the operands of logical operations are the same

int foo(int a)
{
  if (a < 0 && a < 0)
    return 0;
  return 1;
}

  • -Wnull-dereference
    Warn when the compiler detects paths that dereferences a null pointer.
void foo(int *p, int a)
{
  int *q = 0;
  if (0 <= a && a < 10)
    q = p + a;
  *q = 1;  // q may be NULL
}

All examples from:
https://kristerw.blogspot.com/2017/09/useful-gcc-warning-options-not-enabled.html

There are some additional warning flags that are not part of -Wall and -Wextra, which might add some useful checks. Some examples: * -Wduplicated-cond Warn about duplicated condition in if-else-if chains, such as ``` int foo(int a) { int b; if (a == 0) b = 42; else if (a == 0) b = 43; return b; } ``` * -Wlogical-op Warn about use of logical operations where a bitwise operation probably was intended, such as ``` int foo(int a) { a = a || 0xf0; return a; } ``` It also warns when the operands of logical operations are the same ``` int foo(int a) { if (a < 0 && a < 0) return 0; return 1; } ``` * -Wnull-dereference Warn when the compiler detects paths that dereferences a null pointer. ``` void foo(int *p, int a) { int *q = 0; if (0 <= a && a < 10) q = p + a; *q = 1; // q may be NULL } ``` All examples from: https://kristerw.blogspot.com/2017/09/useful-gcc-warning-options-not-enabled.html
gaisser added the
feature
label 2021-07-19 15:31:54 +02:00
Owner
From our friends at Airbus: https://airbus-seclab.github.io/c-compiler-security/
Owner

From #491: -Wundef might be useful to catch missing configuration defines

From #491: `-Wundef` might be useful to catch missing configuration defines
gaisser was assigned by mohr 2022-02-07 14:56:40 +01:00
gaisser added this to the v5.0.0 milestone 2022-02-23 22:43:31 +01:00
Author
Owner

Merged in #568

Merged in #568
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: fsfw/fsfw#446
No description provided.