diff options
author | Kjell Winblad <[email protected]> | 2019-01-29 12:33:19 +0100 |
---|---|---|
committer | Kjell Winblad <[email protected]> | 2019-01-30 14:38:25 +0100 |
commit | 1df3d85824601e3c07d12ca9811866c2ef334e76 (patch) | |
tree | e6de0e195b436e69aee3af23ec6a890d43636ab4 /erts/emulator | |
parent | 71e7b98034ac00d9ee5428ed0602646e29851a29 (diff) | |
download | otp-1df3d85824601e3c07d12ca9811866c2ef334e76.tar.gz otp-1df3d85824601e3c07d12ca9811866c2ef334e76.tar.bz2 otp-1df3d85824601e3c07d12ca9811866c2ef334e76.zip |
Fix bug in binary:encode_unsigned causing a read of uninitialized memory
The bug could be seen by running the test that is added by this commit
in a valgrind enabled emulator.
Co-authored-by: John Högberg <[email protected]>
Diffstat (limited to 'erts/emulator')
-rw-r--r-- | erts/emulator/beam/erl_bif_binary.c | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/erts/emulator/beam/erl_bif_binary.c b/erts/emulator/beam/erl_bif_binary.c index a2610bf2e1..ae1bf6e652 100644 --- a/erts/emulator/beam/erl_bif_binary.c +++ b/erts/emulator/beam/erl_bif_binary.c @@ -2762,7 +2762,7 @@ static BIF_RETTYPE do_encode_unsigned(Process *p, Eterm uns, Eterm endianess) dsize_t num_parts = BIG_SIZE(bigp); Eterm res; byte *b; - ErtsDigit d; + ErtsDigit d = 0; if(BIG_SIGN(bigp)) { goto badarg; @@ -2778,26 +2778,22 @@ static BIF_RETTYPE do_encode_unsigned(Process *p, Eterm uns, Eterm endianess) if (endianess == am_big) { Sint i,j; j = 0; - d = BIG_DIGIT(bigp,0); for (i=n-1;i>=0;--i) { - b[i] = d & 0xFF; - if (!((++j) % sizeof(ErtsDigit))) { + if (!((j++) % sizeof(ErtsDigit))) { d = BIG_DIGIT(bigp,j / sizeof(ErtsDigit)); - } else { - d >>= 8; } + b[i] = d & 0xFF; + d >>= 8; } } else { Sint i,j; j = 0; - d = BIG_DIGIT(bigp,0); for (i=0;i<n;++i) { - b[i] = d & 0xFF; - if (!((++j) % sizeof(ErtsDigit))) { + if (!((j++) % sizeof(ErtsDigit))) { d = BIG_DIGIT(bigp,j / sizeof(ErtsDigit)); - } else { - d >>= 8; } + b[i] = d & 0xFF; + d >>= 8; } } |