Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5267

Functions with "noreturn" attribute should not return

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      function "XXX" declared 'noreturn' should not return
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Lexical Analysis

      Description

      The attribute noreturn indicates that a function does not return.
      Using this attribute allows the compiler to do some assumptions that can lead to optimizations. However, if a function with this attribute ever returns, the behavior becomes undefined.

      Noncompliant Code Example

      [[noreturn]] void f () {
        while (1) {
          // ...
          if (/* something*/) {
            return; // Noncompliant, this function should not return
          }
        }
      }
      

      Compliant Solution

      [[noreturn]] void f() {  // Compliant
        while (true) {
          // ...
        }
      }
      

      Or

      void f() {  
        while (true) {
          // ... 
          if (/* something*/) { 
            return; // Compliant 
          }
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              amelie.renard Amélie Renard
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: