summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-12 08:11:36 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-01-12 08:11:36 +0000
commit9bd672f6682aef683c49f0b8da2b439e4d4103ac (patch)
tree17a6f91b7e705c4ba7bf32e6ceadd136e7b595f5
parent42406852371bdf3fe3d2e4e51192c87720a8e568 (diff)
ext: use rb_sprintf() and rb_vsprintf() with PRIsVALUE
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44572 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ext/dl/cptr.c5
-rw-r--r--ext/fiddle/pointer.c6
-rw-r--r--ext/openssl/ossl.c18
-rw-r--r--ext/pty/pty.c6
-rw-r--r--ext/strscan/strscan.c47
-rw-r--r--ext/tk/tcltklib.c7
-rw-r--r--ext/win32ole/win32ole.c11
-rw-r--r--ext/zlib/zlib.c7
8 files changed, 48 insertions, 59 deletions
diff --git a/ext/dl/cptr.c b/ext/dl/cptr.c
index 9e59139..b3d7ef9 100644
--- a/ext/dl/cptr.c
+++ b/ext/dl/cptr.c
@@ -391,9 +391,8 @@ rb_dlptr_inspect(VALUE self)
char str[1024];
TypedData_Get_Struct(self, struct ptr_data, &dlptr_data_type, data);
- snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>",
- rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free);
- return rb_str_new2(str);
+ return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
+ CLASS_OF(self), data, data->ptr, data->size, data->free);
}
/*
diff --git a/ext/fiddle/pointer.c b/ext/fiddle/pointer.c
index 4f4842f..ca09c41 100644
--- a/ext/fiddle/pointer.c
+++ b/ext/fiddle/pointer.c
@@ -427,12 +427,10 @@ static VALUE
rb_fiddle_ptr_inspect(VALUE self)
{
struct ptr_data *data;
- char str[1024];
TypedData_Get_Struct(self, struct ptr_data, &fiddle_ptr_data_type, data);
- snprintf(str, 1023, "#<%s:%p ptr=%p size=%ld free=%p>",
- rb_class2name(CLASS_OF(self)), data, data->ptr, data->size, data->free);
- return rb_str_new2(str);
+ return rb_sprintf("#<%"PRIsVALUE":%p ptr=%p size=%ld free=%p>",
+ CLASS_OF(self), data, data->ptr, data->size, data->free);
}
/*
diff --git a/ext/openssl/ossl.c b/ext/openssl/ossl.c
index 689f21a..614d2fa 100644
--- a/ext/openssl/ossl.c
+++ b/ext/openssl/ossl.c
@@ -294,10 +294,9 @@ ossl_to_der_if_possible(VALUE obj)
static VALUE
ossl_make_error(VALUE exc, const char *fmt, va_list args)
{
- char buf[BUFSIZ];
+ VALUE str = Qnil;
const char *msg;
long e;
- int len = 0;
#ifdef HAVE_ERR_PEEK_LAST_ERROR
e = ERR_peek_last_error();
@@ -305,14 +304,19 @@ ossl_make_error(VALUE exc, const char *fmt, va_list args)
e = ERR_peek_error();
#endif
if (fmt) {
- len = vsnprintf(buf, BUFSIZ, fmt, args);
+ str = rb_vsprintf(fmt, args);
}
- if (len < BUFSIZ && e) {
+ if (e) {
if (dOSSL == Qtrue) /* FULL INFO */
msg = ERR_error_string(e, NULL);
else
msg = ERR_reason_error_string(e);
- len += snprintf(buf+len, BUFSIZ-len, "%s%s", (len ? ": " : ""), msg);
+ if (NIL_P(str)) {
+ str = rb_str_new_cstr(msg);
+ }
+ else {
+ rb_str_cat2(rb_str_cat2(str, ": "), msg);
+ }
}
if (dOSSL == Qtrue){ /* show all errors on the stack */
while ((e = ERR_get_error()) != 0){
@@ -321,8 +325,8 @@ ossl_make_error(VALUE exc, const char *fmt, va_list args)
}
ERR_clear_error();
- if(len > BUFSIZ) len = rb_long2int(strlen(buf));
- return rb_exc_new(exc, buf, len);
+ if (NIL_P(str)) str = rb_str_new(0, 0);
+ return rb_exc_new3(exc, str);
}
void
diff --git a/ext/pty/pty.c b/ext/pty/pty.c
index 4ba1cba..f54bbb5 100644
--- a/ext/pty/pty.c
+++ b/ext/pty/pty.c
@@ -613,7 +613,7 @@ static void
raise_from_check(rb_pid_t pid, int status)
{
const char *state;
- char buf[1024];
+ VALUE msg;
VALUE exc;
#if defined(WIFSTOPPED)
@@ -631,8 +631,8 @@ raise_from_check(rb_pid_t pid, int status)
else {
state = "exited";
}
- snprintf(buf, sizeof(buf), "pty - %s: %ld", state, (long)pid);
- exc = rb_exc_new2(eChildExited, buf);
+ msg = rb_sprintf("pty - %s: %ld", state, (long)pid);
+ exc = rb_exc_new_str(eChildExited, msg);
rb_iv_set(exc, "status", rb_last_status_get());
rb_exc_raise(exc);
}
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c
index f020ba7..1db6898 100644
--- a/ext/strscan/strscan.c
+++ b/ext/strscan/strscan.c
@@ -1155,37 +1155,32 @@ static VALUE
strscan_inspect(VALUE self)
{
struct strscanner *p;
- char buf[BUFSIZE];
- long len;
VALUE a, b;
p = check_strscan(self);
if (NIL_P(p->str)) {
- len = snprintf(buf, BUFSIZE, "#<%s (uninitialized)>",
- rb_class2name(CLASS_OF(self)));
- return infect(rb_str_new(buf, len), p);
+ a = rb_sprintf("#<%"PRIsVALUE" (uninitialized)>", CLASS_OF(self));
+ return infect(a, p);
}
if (EOS_P(p)) {
- len = snprintf(buf, BUFSIZE, "#<%s fin>",
- rb_class2name(CLASS_OF(self)));
- return infect(rb_str_new(buf, len), p);
+ a = rb_sprintf("#<%"PRIsVALUE" fin>", CLASS_OF(self));
+ return infect(a, p);
}
if (p->curr == 0) {
- b = inspect2(p);
- len = snprintf(buf, BUFSIZE, "#<%s %ld/%ld @ %s>",
- rb_class2name(CLASS_OF(self)),
- p->curr, S_LEN(p),
- RSTRING_PTR(b));
- return infect(rb_str_new(buf, len), p);
+ b = inspect2(p);
+ a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld @ %"PRIsVALUE">",
+ CLASS_OF(self),
+ p->curr, S_LEN(p),
+ b);
+ return infect(a, p);
}
a = inspect1(p);
b = inspect2(p);
- len = snprintf(buf, BUFSIZE, "#<%s %ld/%ld %s @ %s>",
- rb_class2name(CLASS_OF(self)),
- p->curr, S_LEN(p),
- RSTRING_PTR(a),
- RSTRING_PTR(b));
- return infect(rb_str_new(buf, len), p);
+ a = rb_sprintf("#<%"PRIsVALUE" %ld/%ld %"PRIsVALUE" @ %"PRIsVALUE">",
+ CLASS_OF(self),
+ p->curr, S_LEN(p),
+ a, b);
+ return infect(a, p);
}
static VALUE
@@ -1210,21 +1205,19 @@ inspect1(struct strscanner *p)
static VALUE
inspect2(struct strscanner *p)
{
- char buf[BUFSIZE];
- char *bp = buf;
+ VALUE str;
long len;
if (EOS_P(p)) return rb_str_new2("");
len = S_LEN(p) - p->curr;
if (len > INSPECT_LENGTH) {
- len = INSPECT_LENGTH;
- memcpy(bp, CURPTR(p), len); bp += len;
- strcpy(bp, "..."); bp += 3;
+ str = rb_str_new(CURPTR(p), INSPECT_LENGTH);
+ rb_str_cat2(str, "...");
}
else {
- memcpy(bp, CURPTR(p), len); bp += len;
+ str = rb_str_new(CURPTR(p), len);
}
- return rb_str_dump(rb_str_new(buf, bp - buf));
+ return rb_str_dump(str);
}
/* =======================================================================
diff --git a/ext/tk/tcltklib.c b/ext/tk/tcltklib.c
index 6f9aa90..5d20b25 100644
--- a/ext/tk/tcltklib.c
+++ b/ext/tk/tcltklib.c
@@ -848,15 +848,14 @@ create_ip_exc(interp, exc, fmt, va_alist)
#endif
{
va_list args;
- char buf[BUFSIZ];
+ VALUE msg;
VALUE einfo;
struct tcltkip *ptr = get_ip(interp);
va_init_list(args,fmt);
- vsnprintf(buf, BUFSIZ, fmt, args);
- buf[BUFSIZ - 1] = '\0';
+ msg = rb_vsprintf(fmt, args);
va_end(args);
- einfo = rb_exc_new2(exc, buf);
+ einfo = rb_exc_new_str(exc, msg);
rb_ivar_set(einfo, ID_at_interp, interp);
if (ptr) {
Tcl_ResetResult(ptr->ip);
diff --git a/ext/win32ole/win32ole.c b/ext/win32ole/win32ole.c
index 9765929..072638a 100644
--- a/ext/win32ole/win32ole.c
+++ b/ext/win32ole/win32ole.c
@@ -1209,19 +1209,18 @@ static void
ole_raise(HRESULT hr, VALUE ecs, const char *fmt, ...)
{
va_list args;
- char buf[BUFSIZ];
+ VALUE msg;
VALUE err_msg;
va_init_list(args, fmt);
- vsnprintf(buf, BUFSIZ, fmt, args);
+ msg = rb_vsprintf(fmt, args);
va_end(args);
err_msg = ole_hresult2msg(hr);
if(err_msg != Qnil) {
- rb_raise(ecs, "%s\n%s", buf, StringValuePtr(err_msg));
- }
- else {
- rb_raise(ecs, "%s", buf);
+ rb_str_cat2(msg, "\n");
+ rb_str_append(msg, err_msg);
}
+ rb_exc_raise(rb_exc_new_str(ecs, msg));
}
void
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index df2a250..391bec1 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -337,11 +337,8 @@ raise_zlib_error(int err, const char *msg)
rb_sys_fail(msg);
/* no return */
default:
- {
- char buf[BUFSIZ];
- snprintf(buf, BUFSIZ, "unknown zlib error %d: %s", err, msg);
- exc = rb_exc_new2(cZError, buf);
- }
+ exc = rb_exc_new_str(cZError,
+ rb_sprintf("unknown zlib error %d: %s", err, msg));
}
rb_exc_raise(exc);