jonasschnelli / bitcoin_txref_code

Confirmed Transaction Reference Code
6 stars 11 forks source link

Code Produces Different Results Depending on If It's Compiled Optimized #2

Open shannona opened 7 years ago

shannona commented 7 years ago

The encode code produces different results depending on whether it's been compiled with an optimized flag or not.

Optimized:

$ cc -O2 txid2bech32.c txref_code.c segwit_addr.c -lbitcoinrpc -ljansson -o txid2bech32
$ ./txid2bech32 f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107
Txref: txtest1-xyv2-xzyq-qqm5-tyke

Non-Optimized:

$ cc txid2bech32.c txref_code.c segwit_addr.c -lbitcoinrpc -ljansson -o txid2bech32
$ ./txid2bech32 f8cdaff3ebd9e862ed5885f8975489090595abe1470397f79780ead1c7528107
Txref: txtest1-xxyv-xzxz-qqmq-tyke

The optimized results are the correct ones.

The non-optimized bug occurs in the hyphenation memcopy codes, starting with this line:

memcpy(output+olen-5, output+olen-8, 4);

The problem seems to occur due to the overlapping copy.

Here's the results of that line:

Optimized

txtest1xyv2xzyqqqm5tyketyke
txtest1xyv2xzyqqqmqqm5etyke

Non-Optimized:

txtest1xyv2xzyqqqm5tyketyke
txtest1xyv2xzyqqqmqqmqetyke