From b4406583e6fe03f92fe2672a7d611359464f9c49 Mon Sep 17 00:00:00 2001 From: knu Date: Thu, 16 Jan 2003 18:48:20 +0000 Subject: [PATCH] * sprintf.c (rb_f_sprintf): Fix a bug caused by an uninitialized variable v, that a bignum unexpectedly gets converted into a string with its higher figures all filled with ./f/7/1, depending on the base. This bug seems to have been introduced in rev.1.27. * sprintf.c (rb_f_sprintf): Use switch instead of a sequence of else-if's. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3349 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ sprintf.c | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9c0d86c8e4..cd1193a5cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Fri Jan 17 03:33:42 2003 Akinori MUSHA + + * sprintf.c (rb_f_sprintf): Fix a bug caused by an uninitialized + variable v, that a bignum unexpectedly gets converted into a + string with its higher figures all filled with ./f/7/1, + depending on the base. This bug seems to have been introduced + in rev.1.27. + + * sprintf.c (rb_f_sprintf): Use switch instead of a sequence of + else-if's. + Wed Jan 15 15:18:38 2003 moumar * configure.in (ARCHFILE): set even unless --enable-shared on diff --git a/sprintf.c b/sprintf.c index d97534de97..2d5f13e372 100644 --- a/sprintf.c +++ b/sprintf.c @@ -319,7 +319,7 @@ rb_f_sprintf(argc, argv) char *prefix = 0; int sign = 0; char sc = 0; - long v; + long v = 0; int base, bignum = 0; int len, pos; @@ -338,11 +338,18 @@ rb_f_sprintf(argc, argv) break; } if (flags & FSHARP) { - if (*p == 'o') prefix = "0"; - else if (*p == 'x') prefix = "0x"; - else if (*p == 'X') prefix = "0X"; - else if (*p == 'b') prefix = "0b"; - else if (*p == 'B') prefix = "0B"; + switch (*p) { + case 'o': + prefix = "0"; break; + case 'x': + prefix = "0x"; break; + case 'X': + prefix = "0X"; break; + case 'b': + prefix = "0b"; break; + case 'B': + prefix = "0B"; break; + } if (prefix) { width -= strlen(prefix); } @@ -369,10 +376,21 @@ rb_f_sprintf(argc, argv) goto bin_retry; } - if (*p == 'u' || *p == 'd' || *p == 'i') base = 10; - else if (*p == 'x' || *p == 'X') base = 16; - else if (*p == 'o') base = 8; - else if (*p == 'b' || *p == 'B') base = 2; + switch (*p) { + case 'o': + base = 8; break; + case 'x': + case 'X': + base = 16; break; + case 'b': + case 'B': + base = 2; break; + case 'u': + case 'd': + case 'i': + default: + base = 10; break; + } if (!bignum) { if (base == 2) { val = rb_int2big(v); @@ -510,7 +528,7 @@ rb_f_sprintf(argc, argv) PUSH(prefix, plen); } CHECK(prec - len); - if (v < 0) { + if (!bignum && v < 0) { char c = '.'; switch (base) {