Skip to content

Possible undefined behavior in impl_sse/o7_oprofile.c #339

@ssijaric-nv

Description

@ssijaric-nv

In impl_sse/p7_oprofile.c, this could potentially lead to undefined behavior due to the cast of a negative float to an uint8_t:

static uint8_t
biased_byteify(P7_OPROFILE *om, float sc)
{
  uint8_t b;

  sc  = -1.0f * roundf(om->scale_b * sc);                          /* ugh. sc is now an integer cost represented in a float...           */
  b   = (sc > 255 - om->bias_b) ? 255 : (uint8_t) sc + om->bias_b; /* and now we cast, saturate, and bias it to an unsigned char cost... */
  return b;
}

The latest C standard has the following in '6.3.1.4 Real floating and integer' section:

1 When a finite value of standard floating type is converted to an integer type other than bool, the
fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part
cannot be represented by the integer type, the behavior is undefined.65)

We ran into this when building hmmer on X86 with the NVIDIA HPC compiler (nvc).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions