039-fix-integer-overflow-in-float-printf-needed-precision-computation.patch 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. From 70d2687d85c314963cf280759b23fd4573ff0d82 Mon Sep 17 00:00:00 2001
  2. From: Rich Felker <[email protected]>
  3. Date: Wed, 19 Oct 2016 20:17:16 -0400
  4. Subject: fix integer overflow in float printf needed-precision computation
  5. if the requested precision is close to INT_MAX, adding
  6. LDBL_MANT_DIG/3+8 overflows. in practice the resulting undefined
  7. behavior manifests as a large negative result, which is then used to
  8. compute the new end pointer (z) with a wildly out-of-bounds value
  9. (more overflow, more undefined behavior). the end result is at least
  10. incorrect output and character count (return value); worse things do
  11. not seem to happen, but detailed analysis has not been done.
  12. this patch fixes the overflow by performing the intermediate
  13. computation as unsigned; after division by 9, the final result
  14. necessarily fits in int.
  15. ---
  16. src/stdio/vfprintf.c | 2 +-
  17. 1 file changed, 1 insertion(+), 1 deletion(-)
  18. diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
  19. index e439a07..cd17ad7 100644
  20. --- a/src/stdio/vfprintf.c
  21. +++ b/src/stdio/vfprintf.c
  22. @@ -312,7 +312,7 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
  23. }
  24. while (e2<0) {
  25. uint32_t carry=0, *b;
  26. - int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3+8)/9;
  27. + int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
  28. for (d=a; d<z; d++) {
  29. uint32_t rm = *d & (1<<sh)-1;
  30. *d = (*d>>sh) + carry;
  31. --
  32. cgit v0.11.2