Skip to content

Commit f748b91

Browse files
committed
Fix infinite loop at illegal sequence [Bug #17729]
As mblen returns -1 on failure, skip the first byte and try the succeeding bytes in that case. Close #4281
1 parent cc281bd commit f748b91

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

eval_intern.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,16 @@ VALUE rb_ec_backtrace_location_ary(const rb_execution_context_t *ec, long lev, l
302302

303303
#ifndef CharNext /* defined as CharNext[AW] on Windows. */
304304
# ifdef HAVE_MBLEN
305-
# define CharNext(p) ((p) + mblen((p), RUBY_MBCHAR_MAXSIZE))
305+
# define CharNext(p) rb_char_next(p)
306+
static inline const char *
307+
rb_char_next(const char *p)
308+
{
309+
if (p) {
310+
int len = mblen(p, RUBY_MBCHAR_MAXSIZE);
311+
p += len > 0 ? len : 1;
312+
}
313+
return p;
314+
}
306315
# else
307316
# define CharNext(p) ((p) + 1)
308317
# endif

test/ruby/test_rubyoptions.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,11 @@ def test_cwd_encoding
10891089
end
10901090
end
10911091

1092+
def test_rubylib_invalid_encoding
1093+
env = {"RUBYLIB"=>"\xFF", "LOCALE"=>"en_US.UTF-8", "LC_ALL"=>"en_US.UTF-8"}
1094+
assert_ruby_status([env, "-e;"])
1095+
end
1096+
10921097
def test_null_script
10931098
skip "#{IO::NULL} is not a character device" unless File.chardev?(IO::NULL)
10941099
assert_in_out_err([IO::NULL], success: true)

0 commit comments

Comments
 (0)