Skip to content

mp_fwrite should not write \0 character #543

@J08nY

Description

@J08nY

Currently, mp_fwrite writes a terminating \0 character to the stream it is given. This does not make much sense, as usually one uses this fwrite to print something to the stdout or to a text file where no zero bytes should appear.

This is due to the mp_to_radix function always appending the NULL byte and the subsequent write in fwrite taking it all.
An easy solution would be to trim the NULL byte during the fwrite call:

https://github.com/libtom/libtommath/blob/develop/mp_fwrite.c#L23

   if ((err = mp_to_radix(a, buf, size, &written, radix)) == MP_OKAY) {
      if (fwrite(buf, written - 1, 1uL, stream) != 1uL) {
         err = MP_ERR;
      }
   }

instead of

   if ((err = mp_to_radix(a, buf, size, &written, radix)) == MP_OKAY) {
      if (fwrite(buf, written, 1uL, stream) != 1uL) {
         err = MP_ERR;
      }
   }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions