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

"SY-SUBRC" should be tested after each statement setting it.

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      * SY-SUBRC must be tested after each call to "XXX"
      * SY-SUBRC must be tested after calling (OPEN|READ|DELETE) DATASET
      Show
      * SY-SUBRC must be tested after each call to "XXX" * SY-SUBRC must be tested after calling (OPEN|READ|DELETE) DATASET
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      ABAP
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min

      Description

      The system field SY-SUBRC must be tested immediately after any statement setting this variable. Reading this variable informs on previous operation success or errors. Such errors should be handled properly so that the program continues in a consistent state.

      This rule raises an issue when the field SY-SUBRC is not checked just after performing one of the following operations:

      • Calling a function or method which can throw exceptions.
      • Calling one of the file access operation OPEN DATASET, READ DATASET or DELETE DATASET.

      SY-SUBRC check must be done either with the CASE, IF or CHECK statement.

      Noncompliant Code Example

      In the following case nothing happens if the exceptions NOT_FOUND or OTHERS are raised:

      CALL FUNCTION 'STRING_SPLIT'
        EXPORTING
          DELIMITER = ':'
          STRING = FELD
        IMPORTING
          HEAD =   HEAD
          TAIL = TAIL
        EXCEPTIONS
          NOT_FOUND = 1
          OTHERS = 2.
      

      Compliant Solution

      CALL FUNCTION 'STRING_SPLIT'
        EXPORTING
          DELIMITER = ':'
          STRING = FELD
        IMPORTING
          HEAD =   HEAD
          TAIL = TAIL
        EXCEPTIONS
          NOT_FOUND = 1
          OTHERS = 2.
      CASE SY-SUBRC.
        WHEN 1. ...
        WHEN 2. ...
        WHEN OTHER.
      ENDCASE.
      

      Exceptions

      No issue will be raised in the following cases:

      • One or more WRITE operation are performed between the statement setting SY-SUBRC and its check. An exception will be however raised if the WRITE operation is a WRITE ... TO, as this will set SY-SUBRC too.
      • SY-SUBRC's value is assigned to a variable. We then assume that it will be checked later.
      OPEN DATASET my_dataset FOR INPUT IN TEXT MODE ENCODING DEFAULT. " Compliant
      WRITE 'Test'. " WRITE is accepted before checking SY-SUBRC
      IF SY-SUBRC <> 0.
          EXIT.
      ENDIF.
      
      OPEN DATASET my_dataset FOR INPUT IN TEXT MODE ENCODING DEFAULT. " Compliant
      Tmp = SY-SUBRC. " Assigning SY-SUBRC value to a variable. We assume that it will be checked later.
      IF Tmp <> 0.
          EXIT.
      ENDIF.
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              freddy.mallet Freddy Mallet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: