Undefined behaviour causing problems on AVR32

Torbjorn Granlund tg at gmplib.org
Mon Apr 13 14:02:20 CEST 2009

Bradley Smith <bradsmith at debian.org> writes:

  In scanf/sscanffuns.c, the function 'scan' is defined as:
   static int scan (const char **sp, const char *fmt, void *p1, void *p2)
  but is then casted to:
   int (*gmp_doscan_scan_t)  _PROTO ((void *, const char *, ...))
  and used. Whilst most architectures seem to cope with this, it is
  undefined behaviour and due to the way varargs are implemented on AVR32,
  it causes segfaults/incorrect results.
  I have attached a simple fix for this which simply changes 'scan' to use
  varargs as in the gmp_doscan_scan_t prototype.
  (This is with the Debian packaged version gmp-4.2.4+dfsg, which is what
  the patch has been created against).
I agree this is broken in GMP.

I don't fully understand the GMP scanf code.

I find two calls to sscanffuns.c's scan through the ->scan field, both
from __gmp_doscan.  One call uses 3 parameters and the other uses 4

Both the current and your suggested code assumes there are 4 parameters.
While your code is an improvement, it still looks undefined to me.

Do you agree?


More information about the gmp-bugs mailing list