Menu

#728 divide by zero don't generate an exception

GC 4.0
accepted
nobody
None
5 - default
2021-06-28
2021-06-14
No

Hi GnuCOBOL team,

if you divide a vriable by zero and try to use an exception via CALL "CBL_ERROR_PROC" it seems not to work.

We got two question regarding this issue:

  1. Is it possible to get the testcase prodeuce "passed"?
  2. Is it possible to get a runtime abort in general while dividing a variable by zero?

You can reproduce it with the attached testcase while using the script tc2243.sh:

cobc -x tc2243.cbl
cobc tc2243UP.cob

./tc2243

The tc2243 program shows a message at the end if the test was passed (CALL of subprogram done) or not passed (divide by zero = 0).

Please take a look

Kind regards

Sebastian Steinweg
EasiRun Europa GmbH

4 Attachments

Discussion

  • Simon Sobisch

    Simon Sobisch - 2021-06-14

    In general every runtime exception is (per COBOL standard) only active upon explicit request.
    The "old" request is --debug, which is the same as the new-fec=all (+ -fstack-check); to include the location of any exception -fsource-location needs to be active (default with both --debug and any -fec) - or the new >> TURN directive to set it "in the code".

    You could also toggle only zero-devide checks with -fec=SIZE-ZERO-DIVIDE (or its group -fec=size).

    In any case an EC-SIZE exception within a numeric statement could also be handled "inline" with the ON SIZE EXCEPTION phase.

    ... so far the theory.

    The current implementation in 3.1.2 does set the exception, but in most cases without taking the compile-time switch into account; and not handling it as fatal error, so commonly the error procedure won't be called; GC 4 partially sets a fatal error (so the handler will be called), but not in all cases checking the compile-time switch.

    As any change in this would be a change in the compiler and runtime behavior it needs to be postponed to 4.x - but if you start a new project and/or always want this to "run fatal":

    • compile with -fec=SIZE-ZERO-DIVIDE (or more checks; this will ensure both that -fsource-location is active and will provide the check in future versions)
    • patch both places in libcob (1x intrinsic.c, 1x numeric.c) that handle this as follows:
                cob_set_exception (COB_EC_SIZE_ZERO_DIVIDE);
    +           cob_fatal_error (COB_FERROR_DIV_ZERO);  // additional to the line above
    
     
  • Simon Sobisch

    Simon Sobisch - 2021-06-14
    • status: open --> accepted
    • Group: GC 3.1 --> GC 4.0
     
  • Simon Sobisch

    Simon Sobisch - 2021-06-14

    @ssteinweg could you please recheck if this works "as expected" for you after applying the diff above?

     
  • Sebastian Steinweg

    There occurs an error during the make process
    numeric.c:1875:34: error: ‘COB_FERROR_DIV_ZERO’ undeclared (first use in this function); did you mean ‘COB_FERROR_FILE’?
    1875 | cob_fatal_error (COB_FERROR_DIV_ZERO);
    | ^~~~~~~~~~~~~~~~~~~
    There is no definition of COB_FERROR_DIV_ZERO in the common.h where all other COB_FERROR* was defined
    And also no usage of this constant anywhere

    grep -R COB_FERROR_DIV_ZERO *
    libcob/numeric.c: cob_fatal_error (COB_FERROR_DIV_ZERO);
    libcob/intrinsic.c: cob_fatal_error (COB_FERROR_DIV_ZERO);

     

Log in to post a comment.