creachadair / imath

Arbitrary precision integer and rational arithmetic library
Other
129 stars 20 forks source link

mp_int_to_unsigned does not return the number of bytes actually written #20

Closed lhf closed 6 years ago

lhf commented 6 years ago

mp_int_to_unsigned does not return the number of bytes actually written to the buffer.

mp_int_to_unsigned calls s_tobin, which does return the number of bytes actually written to the buffer, so the information is available.

One solution is to change mp_int_to_unsigned to

mp_result mp_int_to_unsigned(mp_int z, unsigned char buf, int limit)

but it is a breaking change.

creachadair commented 6 years ago

One may call mp_int_unsigned_len(&z) to get the length of the buffer consumed, e.g.,

mp_result len = mp_int_unsigned_len(&z);
unsigned char *buf = malloc(len);
mp_int_to_unsigned(&z, buf, len);

I realize that's an extra call, but it's not a very expensive one, so I'd rather not change the API for this.

lhf commented 6 years ago

I misread the docs. Sorry for the noise.