summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-22 01:22:38 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-22 01:22:38 +0000
commit48f695119c6e44851ea39198bd4917745fc0562c (patch)
treeedaa53504cd271f2d091d37e20affdc5f1c5849d
parent9ea0e11df7267be362bf43f9e3fc96b66c5b6150 (diff)
* sprintf.c (rb_f_sprintf): more checks for format argument.
[ruby-core:11569], [ruby-core:11570], [ruby-core:11571], [ruby-core:11573] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@13178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--sprintf.c65
-rw-r--r--version.h2
3 files changed, 44 insertions, 29 deletions
diff --git a/ChangeLog b/ChangeLog
index d2f9016..a5ddd98 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Aug 22 10:20:32 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * sprintf.c (rb_f_sprintf): more checks for format argument.
+ [ruby-core:11569], [ruby-core:11570], [ruby-core:11571],
+ [ruby-core:11573]
+
Wed Aug 22 10:13:45 2007 pegacorn <subscriber.jp AT gmail.com>
* ext/digest/digest.c (rb_digest_instance_update,
diff --git a/sprintf.c b/sprintf.c
index e3b0310..76093fa 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -82,6 +82,7 @@ sign_bits(base, p)
#define FSPACE 16
#define FWIDTH 32
#define FPREC 64
+#define FPREC0 128
#define CHECK(l) do {\
while (blen + (l) >= bsiz) {\
@@ -110,9 +111,7 @@ sign_bits(base, p)
#define GETNTHARG(nth) \
((nth >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[nth])
-#define GETASTER(val) do { \
- t = p++; \
- n = 0; \
+#define GETNUM(n, val) \
for (; p < end && ISDIGIT(*p); p++) { \
int next_n = 10 * n + (*p - '0'); \
if (next_n / 10 != n) {\
@@ -122,7 +121,12 @@ sign_bits(base, p)
} \
if (p >= end) { \
rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \
- } \
+ }
+
+#define GETASTER(val) do { \
+ t = p++; \
+ n = 0; \
+ GETNUM(n, val); \
if (*p == '$') { \
tmp = GETPOSARG(n); \
} \
@@ -257,6 +261,21 @@ rb_f_sprintf(argc, argv)
VALUE tmp;
VALUE str;
+#define CHECK_FOR_WIDTH(f) \
+ if ((f) & FWIDTH) { \
+ rb_raise(rb_eArgError, "width given twice"); \
+ } \
+ if ((f) & FPREC0) { \
+ rb_raise(rb_eArgError, "width after precision"); \
+ }
+#define CHECK_FOR_FLAGS(f) \
+ if ((f) & FWIDTH) { \
+ rb_raise(rb_eArgError, "flag after width"); \
+ } \
+ if ((f) & FPREC0) { \
+ rb_raise(rb_eArgError, "flag after precision"); \
+ }
+
fmt = GETNTHARG(0);
if (OBJ_TAINTED(fmt)) tainted = 1;
StringValue(fmt);
@@ -292,43 +311,40 @@ rb_f_sprintf(argc, argv)
break;
case ' ':
+ CHECK_FOR_FLAGS(flags);
flags |= FSPACE;
p++;
goto retry;
case '#':
+ CHECK_FOR_FLAGS(flags);
flags |= FSHARP;
p++;
goto retry;
case '+':
+ CHECK_FOR_FLAGS(flags);
flags |= FPLUS;
p++;
goto retry;
case '-':
+ CHECK_FOR_FLAGS(flags);
flags |= FMINUS;
p++;
goto retry;
case '0':
+ CHECK_FOR_FLAGS(flags);
flags |= FZERO;
p++;
goto retry;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
+ CHECK_FOR_WIDTH(flags);
n = 0;
- for (; p < end && ISDIGIT(*p); p++) {
- int next_n = 10 * n + (*p - '0');
- if (next_n / 10 != n) {
- rb_raise(rb_eArgError, "width too big");
- }
- n = 10 * n + (*p - '0');
- }
- if (p >= end) {
- rb_raise(rb_eArgError, "malformed format string - %%[0-9]");
- }
+ GETNUM(n, width);
if (*p == '$') {
if (nextvalue != Qundef) {
rb_raise(rb_eArgError, "value given twice - %d$", n);
@@ -342,10 +358,7 @@ rb_f_sprintf(argc, argv)
goto retry;
case '*':
- if (flags & FWIDTH) {
- rb_raise(rb_eArgError, "width given twice");
- }
-
+ CHECK_FOR_WIDTH(flags);
flags |= FWIDTH;
GETASTER(width);
if (width < 0) {
@@ -356,10 +369,10 @@ rb_f_sprintf(argc, argv)
goto retry;
case '.':
- if (flags & FPREC) {
+ if (flags & FPREC0) {
rb_raise(rb_eArgError, "precision given twice");
}
- flags |= FPREC;
+ flags |= FPREC|FPREC0;
prec = 0;
p++;
@@ -372,17 +385,12 @@ rb_f_sprintf(argc, argv)
goto retry;
}
- for (; p < end && ISDIGIT(*p); p++) {
- prec = 10 * prec + (*p - '0');
- }
- if (p >= end) {
- rb_raise(rb_eArgError, "malformed format string - %%.[0-9]");
- }
+ GETNUM(prec, precision);
goto retry;
case '\n':
- p--;
case '\0':
+ p--;
case '%':
if (flags != FNONE) {
rb_raise(rb_eArgError, "illegal format character - %%");
@@ -455,7 +463,7 @@ rb_f_sprintf(argc, argv)
{
volatile VALUE val = GETARG();
char fbuf[32], nbuf[64], *s, *t;
- char *prefix = 0;
+ const char *prefix = 0;
int sign = 0;
char sc = 0;
long v = 0;
@@ -532,6 +540,7 @@ rb_f_sprintf(argc, argv)
default:
base = 10; break;
}
+
if (!bignum) {
if (base == 2) {
val = rb_int2big(v);
diff --git a/version.h b/version.h
index 1724049..bf33a95 100644
--- a/version.h
+++ b/version.h
@@ -2,7 +2,7 @@
#define RUBY_RELEASE_DATE "2007-08-22"
#define RUBY_VERSION_CODE 186
#define RUBY_RELEASE_CODE 20070822
-#define RUBY_PATCHLEVEL 65
+#define RUBY_PATCHLEVEL 66
#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 8