Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add errno check to u_collate_n

  • Loading branch information...
commit 7e117420765d16216e1e67e5fdea16eaa098f5f0 1 parent 88b527c
Nikolai Weibull authored
View
25 ext/u/rb_private.c
@@ -47,6 +47,31 @@ rb_u_raise(VALUE exception, const char *format, ...)
rb_exc_raise(rb_exc_new3(exception, message));
}
+void
+rb_u_raise_errno(VALUE exception, int number, const char *format, ...)
+{
+ va_list args;
+ VALUE message;
+
+ va_start(args, format);
+#ifdef HAVE_RUBY_ENCODING_H
+ message = rb_enc_vsprintf(rb_utf8_encoding(), format, args);
+#else
+ message = rb_vsprintf(format, args);
+#endif
+ va_end(args);
+
+ VALUE error = rb_str_new2(strerror(number));
+#ifdef HAVE_RUBY_ENCODING_H
+ rb_enc_associate(error, rb_locale_encoding());
+ error = rb_str_encode(error, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil);
+#endif
+
+ rb_exc_raise(rb_exc_new3(exception,
+ rb_str_append(rb_str_append(message, rb_str_new2(": ")),
+ error)));
+}
+
VALUE
rb_u_str_new(const char *string, long length)
{
View
1  ext/u/rb_private.h
@@ -42,6 +42,7 @@ int rb_memhash(const char *string, long length);
long rb_u_memsearch(const void *a, long a_n, const void *b, long b_n);
NORETURN(void rb_u_raise(VALUE exception, const char *format, ...)) PRINTF(2, 3);
+NORETURN(void rb_u_raise_errno(VALUE exception, int number, const char *format, ...)) PRINTF(3, 4);
VALUE rb_u_str_new(const char *string, long length);
VALUE rb_u_str_buf_new(long length);
View
9 ext/u/rb_u_string_collate.c
@@ -1,4 +1,5 @@
#include "rb_includes.h"
+#include <errno.h>
/* @overload <=>(other)
*
@@ -19,6 +20,10 @@ rb_u_string_collate(VALUE self, VALUE rbother)
const struct rb_u_string *string = RVAL2USTRING(self);
const struct rb_u_string *other = RVAL2USTRING_ANY(rbother);
- return INT2FIX(u_collate_n(USTRING_STR(string), USTRING_LENGTH(string),
- USTRING_STR(other), USTRING_LENGTH(other)));
+ errno = 0;
+ int r = u_collate_n(USTRING_STR(string), USTRING_LENGTH(string),
+ USTRING_STR(other), USTRING_LENGTH(other));
+ if (errno != 0)
+ rb_u_raise_errno(rb_eSystemCallError, errno, "can’t collate strings");
+ return INT2FIX(r);
}
Please sign in to comment.
Something went wrong with that request. Please try again.