https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98549

--- Comment #19 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Segher Boessenkool from comment #12)
> for (long i; i != compress_n_blocks; ++i)

It's not the code I pasted:

>  for (long i = 0; i < compress_n_blocks; ++i) {
>    unsigned char S[32], S2[32];

I initialized 'i' to zero.

> 
> "i" is uninitialized; accessing it is UB.  So this is ice-on-invalid.
> 
> I have no doubt there is an actual bug somewhere here.  We just do not
> have valid code yet as testcase (preferably shorter than this, and C
> code, so that it is easier and can run on more systems).

There's a code snippet that should not contain UBSAN:

extern "C" void *memcpy(void *, const void *, unsigned long);
inline void copy_mem(unsigned char *out, unsigned char *in, long n) {
  memcpy(out, in, n);
}
template <typename T> void typecast_copy(unsigned char out[], T in, long N) {
  memcpy(out, in, sizeof(T) * N);
}
template <typename T> void typecast_copy(T out, unsigned char in[], long N) {
  memcpy(out, in, sizeof(T) * N);
}

void encrypt_me(unsigned char *ptr);

unsigned char *xor_buf_in;
class BlockCipher {
public:
  void encrypt(unsigned char *ptr);
} * compress_n___trans_tmp_2;
class GOST_34_11 {
  void compress_n();
  BlockCipher m_cipher;
};

long compress_n_blocks = 0;
void GOST_34_11::compress_n() {
  for (long i = 0; i < compress_n_blocks; ++i) {
    unsigned char S[32], S2[32];
    char S_1;
    m_cipher.encrypt(S);
    S2[13] = S_1;
    long x[4], y[4];
    typecast_copy(x, S2, 4);
    x[0] ^= y[0];
    x[1] ^= y[1];
    x[2] ^= x[3];
    typecast_copy(S, x, 4);
    S2[0] = S[4] ^ S[6] ^ 4 ^ S[30];
    copy_mem(S, S + 2, 30);
    S[30] = S2[0];
    {
      long x[4];
      typecast_copy(x, S, 4);
      typecast_copy(y, xor_buf_in, 4);
      x[0] ^= y[0];
      typecast_copy(S, x, 4);
    }
    S2[4] = S[6];
    memcpy(compress_n___trans_tmp_2, S2, 32);
  }
}

int main()
{
  return 0;
}

Reply via email to