summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-01-16 18:48:20 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-01-16 18:48:20 +0000
commitb4406583e6fe03f92fe2672a7d611359464f9c49 (patch)
tree7b1af6fe8d6a84e4bfb3ad5c2b99c4991e79113f /sprintf.c
parent0df980f610dcf6b72b3236a1eeb3b77c29978e1a (diff)
* 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
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c40
1 files changed, 29 insertions, 11 deletions
diff --git a/sprintf.c b/sprintf.c
index d97534d..2d5f13e 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) {