IBM Support

LI75586: C COMPILER ISSUE WITH CONST INT ARRAY SIZES

Subscribe

You can track all active APARs for this component.

 

APAR status

  • Closed as program error.

Error description

  • In c99, when a variable declared as 'const int' is used as size
    in a local array declaration, the size determination and space
    allocation under some conditions is mishandled, and space is not
    allocated.
    
    ===== COMPILE COMMAND:
    xlc test.c
    
    $ cat xlc-bug.c
    #include <stdio.h>
    #include <string.h>
    
    enum { CRDAPPU_CDS_NAMES_MAX_TOKEN_SIZE = 50 };
    typedef char
    crdappu_cds_names_token_t[CRDAPPU_CDS_NAMES_MAX_TOKEN_SIZE];
    
    void write_stuff(crdappu_cds_names_token_t names[]);
    void test_one();
    void test_two();
    
    int main(int argc, char *argv[]){
      if ( argc <= 1) {
        printf("calling test_two\n");
        test_two();
        printf("back from test_two\n");
      }
      else {
        printf("calling test_one\n");
        test_one();
        printf("back test_one\n");
      }
    
      return 0;
    }
    
    
    /*
    ** If we use enum it works
    enum { cdsNamesTokenArrSz = 100 };
    **  With the following, test_two crashes
    **  if it executes before test_one executes
    */
    const int cdsNamesTokenArrSz = 100;
    
    void test_one(){
      char my_array[CRDAPPU_CDS_NAMES_MAX_TOKEN_SIZE];
      char my_array1[CRDAPPU_CDS_NAMES_MAX_TOKEN_SIZE];
      crdappu_cds_names_token_t
    cdsNamesTokenArr[cdsNamesTokenArrSz];
    printf("sizeof my_array = %ld\n", sizeof my_array);
    printf("sizeof my_array1 = %ld\n", sizeof my_array1);
    printf("sizeof cdsNamesTokenArr = %ld\n", sizeof
    cdsNamesTokenArr);
      memset(my_array,' ',sizeof my_array);
      memset(my_array1,' ',sizeof my_array1);
      write_stuff(cdsNamesTokenArr);
    printf("after write_stuff call\n");
      memset(my_array,'1',sizeof my_array);
    printf("after memset to my_array\n");
      memset(my_array1,'1',sizeof my_array1);
    printf("after memset to my_array1\n");
    }
    
    void test_two(){
      char my_array[CRDAPPU_CDS_NAMES_MAX_TOKEN_SIZE];
      char my_array1[CRDAPPU_CDS_NAMES_MAX_TOKEN_SIZE];
      crdappu_cds_names_token_t
    cdsNamesTokenArr[cdsNamesTokenArrSz];
    printf("sizeof my_array = %ld\n", sizeof my_array);
    printf("sizeof my_array1 = %ld\n", sizeof my_array1);
    printf("sizeof cdsNamesTokenArr = %ld\n", sizeof
    cdsNamesTokenArr);
      memset(my_array,' ',sizeof my_array);
      memset(my_array1,' ',sizeof my_array1);
      write_stuff(cdsNamesTokenArr);
    printf("after write_stuff call\n");
      memset(my_array,'1',sizeof my_array);
    printf("after memset to my_array\n");
      memset(my_array1,'1',sizeof my_array1);
    printf("after memset to my_array1\n");
    }
    
    void write_stuff(  crdappu_cds_names_token_t names[]){
      int i,k;
      printf("write_stuff starts\n");
    printf("sizeof names = %ld\n", sizeof names);
    printf("sizeof names[0] = %ld\n", sizeof names[0]);
    printf("sizeof(crdappu_cds_names_token_t) = %ld\n",
    sizeof(crdappu_cds_names_token_t));
      for(i=0;i<100;i++){
        for(k=0;k<sizeof(crdappu_cds_names_token_t);k++){
          names[i][k]='L';
    
        }
    
      }
      printf("write_stuff ends\n");
    }
    $
    
    
    ===== ACTUAL OUTPUT:
    $
    /.../torolab.ibm.com/fs/projects/vabld/run/vacpp/101_com/aix/dai
    ly/latest/bin/xlc xlc-bug.c
    $ ./a.out
    calling test_two
    sizeof my_array = 50
    sizeof my_array1 = 50
    sizeof cdsNamesTokenArr = 0
    write_stuff starts
    sizeof names = 4
    sizeof names[0] = 50
    sizeof(crdappu_cds_names_token_t) = 50
    write_stuff ends
    after write_stuff call
    after memset to my_array
    after memset to my_array1
    Segmentation fault
    $
    
    
    
    ===== EXPECTED OUTPUT:
    $ ./a.out
    calling test_two
    sizeof my_array = 50
    sizeof my_array1 = 50
    sizeof cdsNamesTokenArr = 5000
    write_stuff starts
    sizeof names = 4
    sizeof names[0] = 50
    sizeof(crdappu_cds_names_token_t) = 50
    write_stuff ends
    after write_stuff call
    after memset to my_array
    after memset to my_array1
    back from test_two
    $
    

Local fix

  • Compile the program as C++.
    

Problem summary

  • USERS AFFECTED:
    Any users using variable length arrays at different scopes in
    different orders from sequential order maybe affected by this
    issue.
    
    PROBLEM DESCRIPTION:
    Variable length arrays that are accessed or defined in
    different a order than code order and at different scopes will
    cause invalid runtime values or protection exceptions.
    

Problem conclusion

  • The compiler has been modified so that the variable length
    array size data is scope dependent. Any time a the VLA goes out
    of scope, the size data is cleared and
    so it will be recalculated upon new VLA definition when it
    comes into scope again.
    

Temporary fix

Comments

APAR Information

  • APAR number

    LI75586

  • Reported component name

    XL C/C++ SLES10

  • Reported component ID

    5724U8300

  • Reported release

    A10

  • Status

    CLOSED PER

  • PE

    NoPE

  • HIPER

    NoHIPER

  • Special Attention

    NoSpecatt

  • Submitted date

    2010-07-28

  • Closed date

    2010-07-28

  • Last modified date

    2010-07-28

  • APAR is sysrouted FROM one or more of the following:

    IZ69754

  • APAR is sysrouted TO one or more of the following:

Fix information

  • Fixed component name

    XL C/C++ SLES10

  • Fixed component ID

    5724U8300

Applicable component levels

[{"Business Unit":{"code":"BU054","label":"Systems w\/TPS"},"Product":{"code":"SSJT9L","label":"XL C\/C++"},"Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"A.1","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
17 October 2021