c++ - std::ofstream prints extra bytes when given an array of unsigned char, but only in Release mode -
i'm writing c++ class add digital signature pdf, using openssl generate sha1 hash, print file:
unsigned char hash[sha_digest_length]; sha1((unsigned char *)temp.c_str(), temp.size(), hash); std::ofstream fout("resources/hash.out", std::ios::binary); fout << hash; fout.close();
this code works expected in debug mode. in release mode, fout << hash
prints 30 instead of sha_digest_length = 20 bytes, last 10 of garbage (maybe buffer overflow?)
my current workaround print each character instead of streaming:
for (int i=0; i<sha_digest_length; ++i) fout.put(hash[i]);
which works in both build modes, i'm curious cause stream operator misread length of string. have thoughts?
for record, i'm compiling msvc++ 12 (x86_amd64).
there stream-inserter nul-terminated strings, 1 selected.
but trying output array of fixed length containing arbitrary bytes, different, leading ub.
just use memberfunction write(buffer, size)
instead, designed that.
Comments
Post a Comment