Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

GCC 6: -Wmisleading-indentation vs "goto fail;"

February 26, 2016
David Malcolm

Share:

    I work at Red Hat on GCC, the GNU Compiler Collection. The next major release of GCC, GCC 6, is just around the corner, so I thought I'd post about a new compiler warning I've contributed to it:
    -Wmisleading-indentation.

    One of the more common "gotchas" in C and C++ programming relates to missing braces. For example, in the following:

      if (some_condition ())
        do_foo ();
        do_bar ();
    

    the "do_bar ();" looks like it's guarded by the conditional, but it's actually outside of it.

    Similarly, in this code fragment:

      for (i = 0; i < n; i++)
        sum[i] = a[i] + b[i];
        prod[i] = a[i] * b[i];
    

    the computation of prod[i] is actually outside of the loop, despite what the indentation might suggest.

    Perhaps the most famous example of this is the so-called "goto fail" vulnerability that affected OS X's SSL implementation, aka CVE-2014-1266:

        if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
            goto fail;
            goto fail;
        /* more checking here.  */
      fail:
        /* cleanups */
        return err;
    

    where the 2nd goto fail; was always executed, leading to signature-checking code being skipped, with "err" set to 0, effectively leading to invalid certificates being accepted as valid.

    This felt to me like something the compiler ought to warn about, so for gcc 6 I've written a new warning: -Wmisleading-indentation.

    The new warning is issued when the indentation of the code might mislead a human reader about the code's true block structure.

    For example, given CVE-2014-1266 above, gcc 6 will issue this:

    sslKeyExchange.c: In function 'SSLVerifySignedServerKeyExchange':
    sslKeyExchange.c:631:8: warning: statement is indented as if it were guarded by... [-Wmisleading-indentation]
            goto fail;
            ^~~~
    sslKeyExchange.c:629:4: note: ...this 'if' clause, but it is not
        if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        ^~
    

    Similarly, for the second example above:

    test.c: In function ‘example_2’:
    test.c:57:5: warning: statement is indented as if it were guarded by... [-Wmisleading-indentation]
         prod[i] = a[i] * b[i];
         ^~~~
    test.c:55:3: note: ...this ‘for’ clause, but it is not
       for (i = 0; i < n; i++)
       ^~~
    

    At a high level, the underlying implementation looks at control statements (if/else, while, for), and if it sees them guard a single statement without braces, it looks at the followup statement. It complains if both have the same indentation.

    That's a simplified description - we spent a fair amount of time working on heuristics in the warning, to try to ensure that it warns for all cases that are reasonable to warn for, whilst not complaining unduly for indentation that's merely bad (rather than being actively misleading). We've also tested it with a variety of coding styles: GNU, K&R, Linux kernel, etc.

    For example, it will warn about poorly-written one-liners:

      if (flag)
        x++; y++;
    

    like this:

      test.c: In function ‘example_3’:
      test.c:25:10: warning: statement is indented as if it were guarded by... [-Wmisleading-indentation]
         x++; y++;
              ^
      test.c:24:3: note: ...this ‘if’ clause, but it is not
       if (flag)
       ^~
    

    but it doesn't warn for things like:

      void all_on_one_line (void) { foo (0); if (flagA) foo (1); foo (2); foo (3); }
    

    I added the new -Wmisleading-indentation warning to "-Wall" in gcc 6's development tree in December, and it's been finding real world bugs, for example one in elfutils: https://gnu.wildebeest.org/blog/mjw/2016/01/09/looking-forward-to-gcc6-nice-new-warnings/
    and a couple in the Linux kernel's "perf" tool:
    * https://lkml.org/lkml/2015/12/14/460
    * https://lkml.org/lkml/2015/12/14/461

    GCC 6 will be available in Fedora 24:
    https://fedoraproject.org/wiki/Changes/GCC6
    and in other fast-moving distributions.

    Alternatively, if you're feeling adventurous, you can download a development snapshot of gcc 6 from https://gcc.gnu.org/snapshots.html

    Enjoy - I hope you don't find any such warnings in your code!

    Last updated: April 5, 2018

    Recent Posts

    • Cloud bursting with confidential containers on OpenShift

    • Reach native speed with MacOS llama.cpp container inference

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue