Skip to content

Wrong optimizr code generation #5433

Closed
Closed
@ElijahGr

Description

@ElijahGr

Expression (ival = ( int) val) is executed even if safeCast==false

example for this issue: d.cpp

#include <stdio.h>
#include <limits.h>

double nextVal()
{
  static double vals[4]={1, 2, 70203888290798.0, 4};
  static int index;
  index &= 3;
  return vals[index++];
}

void test()
{
  double val = nextVal();
  printf("test %f ", val);
  int ival = 0;
  bool safeCast = (val>=INT_MIN && val<=INT_MAX);
  printf("cast %d ", safeCast);

  if (safeCast)
  {
    ival = ( int) val; //code is always executed even if safeCast==false
  }

  if ((double)ival == val)
  {
    printf("val %f is int\n", val);
    return;
  }
  printf("%f not int\n", val);
}

int main()
{
  test();
  test();
  test(); //raise integer overflow error 
  test();
}

Compile command:
emcc -O2 d.cpp -o d.html -s WASM=1

This error not appears without any optimization or BINARYEN_TRAP_MODE='clamp'.
Looks like the compiler believe that expression (ival = ( int) val) has no side effects this is not the case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions