"implementation defined" 优化导致的行为改变是合法的还是预期的?

Is behavioral change due to optimizations with "implementation defined" legal or expected?

我一直在关注 OpenSSL 邮件列表上的一个话题。帖子标题为 CBC ciphers + TLS 1.0 protocol does not work in OpenSSL 1.0.2d.

OpenSSL 1.0.2d had intermittent problems due to the following。它出现在微软的 WinCE 编译器下。这个想法是将高位传播到所有其他位:

#define DUPLICATE_MSB_TO_ALL(x) ( (unsigned)( (int)(x) >> (sizeof(int)*8-1) ) )
#define DUPLICATE_MSB_TO_ALL_8(x) ((unsigned char)(DUPLICATE_MSB_TO_ALL(x)))

static unsigned char constant_time_eq_8(unsigned a, unsigned b)
{
    unsigned c = a ^ b;
    c--;
    return DUPLICATE_MSB_TO_ALL_8(c);
}

OpenSSL 尝试遵循 C89。我相信这是 implementation defined behavior due to shifting of a negative value on a 2's compliment machine.

但是,OP 发现它受到了优化的影响。 没有 优化代码产生了正确的结果。 通过 优化代码产生了不正确的结果。

我的问题是,当依赖于实现定义的行为时,结果根据优化而改变是合法的还是预期的?

实现定义意味着实现必须记录行为:

implementation-defined behavior

unspecified behavior where each implementation documents how the choice is made

因此,请查阅您观察到意外结果的编译器的文档。该文档可能会也可能不会指定您观察到的行为。

如果观察到的行为与文档不同,则使用 SSCCE 确认并提交错误报告。如果文档不存在,则针对缺少所需文档提交错误报告。