summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-09 09:16:59 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-09 09:16:59 (GMT)
commit067d9e9ce97dd42b8b13eef10031b0fa9cf23a1f (patch)
tree934f07816670ae9286f399b18f9b8b78c495bc16 /sprintf.c
parent6c3c48e3d569a40774a14a5bfbebf3fc72874de0 (diff)
sprintf.c: NULL as str
* sprintf.c (ruby_vsnprintf, ruby_snprintf): allow NULL as str, just count the expected buffer size. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59789 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/sprintf.c b/sprintf.c
index 4ee7e1d..2bd4966 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -1261,21 +1261,29 @@ ruby_ultoa(unsigned long val, char *endp, int base, int flags)
return BSD__ultoa(val, endp, base, octzero, xdigs);
}
+static int ruby_do_vsnprintf(char *str, size_t n, const char *fmt, va_list ap);
+
int
ruby_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{
+ if (str && (ssize_t)n < 1)
+ return (EOF);
+ return ruby_do_vsnprintf(str, n, fmt, ap);
+}
+
+static int
+ruby_do_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
+{
int ret;
rb_printf_buffer f;
- if ((int)n < 1)
- return (EOF);
f._flags = __SWR | __SSTR;
f._bf._base = f._p = (unsigned char *)str;
- f._bf._size = f._w = n - 1;
+ f._bf._size = f._w = str ? (n - 1) : 0;
f.vwrite = BSD__sfvwrite;
f.vextra = 0;
ret = (int)BSD_vfprintf(&f, fmt, ap);
- *f._p = 0;
+ if (str) *f._p = 0;
return ret;
}
@@ -1285,11 +1293,11 @@ ruby_snprintf(char *str, size_t n, char const *fmt, ...)
int ret;
va_list ap;
- if ((int)n < 1)
+ if (str && (ssize_t)n < 1)
return (EOF);
va_start(ap, fmt);
- ret = ruby_vsnprintf(str, n, fmt, ap);
+ ret = ruby_do_vsnprintf(str, n, fmt, ap);
va_end(ap);
return ret;
}