Project

General

Profile

tsafe_inline.patch

arrtchiu (Matt U), 07/29/2014 10:29 AM

Download (3.51 KB)

View differences:

configure.in
2066 2066
AC_CHECK_FUNCS(utimes)
2067 2067
AC_CHECK_FUNCS(wait4)
2068 2068
AC_CHECK_FUNCS(waitpid)
2069
AC_CHECK_FUNCS(consttime_memequal)
2070
AC_CHECK_FUNCS(timingsafe_memcmp)
2069 2071

  
2070 2072
AS_IF([test "$ac_cv_func_getcwd" = yes], [
2071 2073
    AC_CACHE_CHECK(if getcwd allocates buffer if NULL is given, [rb_cv_getcwd_malloc],
string.c
2490 2490
    return str_eql(str1, str2);
2491 2491
}
2492 2492

  
2493
static inline VALUE
2494
rb_tsafe_eql(const char* buf1, const char* buf2, long len)
2495
{
2496
#if defined(HAVE_TIMINGSAFE_MEMCMP)
2497
    return (timingsafe_memcmp(buf1, buf2, len) == 0);
2498
#elif defined(HAVE_CONSTTIME_MEMEQUAL)
2499
    return (consttime_memequal(buf1, buf2, len) != 0);
2500
#else
2501
    VALUE result;
2502
    long idx;
2503

  
2504
    result = 0;
2505
    idx = 0;
2506
    if (UNALIGNED_WORD_ACCESS || !((VALUE)buf1 % sizeof(VALUE)) && !((VALUE)buf2 % sizeof(VALUE))) {
2507
        for (; idx < len; idx += sizeof(VALUE)) {
2508
            result |= *(const VALUE *)(buf1+idx) ^ *(const VALUE *)(buf2+idx);
2509
        }
2510
    }
2511

  
2512
    for (; idx < len; idx++) {
2513
        result |= buf1[idx] ^ buf2[idx];
2514
    }
2515

  
2516
    return (result == 0);
2517
#endif
2518
}
2519

  
2520
/*
2521
 * call-seq:
2522
 *   str.tsafe_eql?(other)   -> true or false
2523
 *
2524
 * Compares each byte of +str+ against +other+, similarly to String#eql?, but
2525
 * performs the comparison in constant-time.
2526
 *
2527
 * This method is timing-safe if both strings are of equal length.
2528
 */
2529

  
2530
static VALUE
2531
rb_str_tsafe_eql(VALUE str1, VALUE str2)
2532
{
2533
    long len;
2534

  
2535
    str2 = StringValue(str2);
2536
    len = RSTRING_LEN(str1);
2537

  
2538
    if (RSTRING_LEN(str2) != len) return Qfalse;
2539
    if (rb_tsafe_eql(RSTRING_PTR(str1), RSTRING_PTR(str2), len) != 0) return Qtrue;
2540

  
2541
    return Qfalse;
2542
}
2543

  
2493 2544
/*
2494 2545
 *  call-seq:
2495 2546
 *     string <=> other_string   -> -1, 0, +1 or nil
......
8761 8812
    rb_define_method(rb_cString, "==", rb_str_equal, 1);
8762 8813
    rb_define_method(rb_cString, "===", rb_str_equal, 1);
8763 8814
    rb_define_method(rb_cString, "eql?", rb_str_eql, 1);
8815
    rb_define_method(rb_cString, "tsafe_eql?", rb_str_tsafe_eql, 1);
8764 8816
    rb_define_method(rb_cString, "hash", rb_str_hash_m, 0);
8765 8817
    rb_define_method(rb_cString, "casecmp", rb_str_casecmp, 1);
8766 8818
    rb_define_method(rb_cString, "+", rb_str_plus, 1);
test/ruby/test_string.rb
304 304
    casetest(S("CaT"), S('cAt'), true) # find these in the case.
305 305
  end
306 306

  
307
  def test_TIMING_SAFE_EQUAL # 'tsafe_eql?'
308
    assert_equal(true, S("foo").tsafe_eql?(S("foo")))
309
    assert_equal(false, S("foo").tsafe_eql?(S("foO")))
310
    assert_equal(true, S("f\x00oo").tsafe_eql?(S("f\x00oo")))
311
    assert_equal(false, S("f\x00oo").tsafe_eql?(S("f\x00oO")))
312
  end
313

  
307 314
  def test_capitalize
308 315
    assert_equal(S("Hello"),  S("hello").capitalize)
309 316
    assert_equal(S("Hello"),  S("hELLO").capitalize)
310
-