Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Case-insensitive substr_compare, ksort, krsort are still locale-dependent

Đang mở Phù hợp với người mới
#24,552 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@lazerg đang làm issue này rồi.

Từ ngày 22/10/2026.

  • #24558 của @lazerg — đang mở
  • #24559 của @sirreal — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
73/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, php
Lĩnh vực
backend, testing

Hướng nghiên cứu

Bắt đầu từ lời gọi substr_compare() đến zend_binary_strncasecmp_l() và hàm so sánh khóa mảng không phân biệt chữ hoa chữ thường php_array_key_compare_string_case_unstable_i() trong php-src; issue xác định cả hai điểm vào và các hàm so sánh thay thế. Kiểm tra các bài kiểm thử hiện có cho substr_compare(), ksort() và krsort() với SORT_STRING | SORT_FLAG_CASE, sau đó bổ sung kiểm thử hồi quy cho locale tiếng Thổ Nhĩ Kỳ. Hoàn thành khi các trường hợp đó khớp với hành vi ASCII không phân biệt chữ hoa chữ thường được mô tả và các bài kiểm thử hiện có đều đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Bug Status: Needs Triage
Description

The following code:

<?php
foreach (['C', 'tr_TR.UTF-8'] as $locale) {
    if (setlocale(LC_CTYPE, $locale) === false) {
        throw new RuntimeException("Locale unavailable: $locale");
    }

    $values = ['I', 'h'];
    sort($values, SORT_STRING | SORT_FLAG_CASE);
    $keys = ['I' => 0, 'h' => 0];
    ksort($keys, SORT_STRING | SORT_FLAG_CASE);

    echo $locale, "\n";
    echo '  stripos:        ', var_export(stripos('I', 'i'), true), "\n";
    echo '  strncasecmp:    ', strncasecmp('I', 'i', 1), "\n";
    echo '  substr_compare: ', substr_compare('I', 'i', 0, 1, true), "\n";
    echo '  sort:           ', implode(',', $values), "\n";
    echo '  ksort:          ', implode(',', array_keys($keys)), "\n";
}

Resulted in this output:

C
  stripos:        0
  strncasecmp:    0
  substr_compare: 0
  sort:           h,I
  ksort:          h,I
tr_TR.UTF-8
  stripos:        0
  strncasecmp:    0
  substr_compare: -32
  sort:           h,I
  ksort:          I,h

But I expected this output instead:

C
  stripos:        0
  strncasecmp:    0
  substr_compare: 0
  sort:           h,I
  ksort:          h,I
tr_TR.UTF-8
  stripos:        0
  strncasecmp:    0
  substr_compare: 0
  sort:           h,I
  ksort:          h,I

Reproduction link


PHP 8.2 switched strtolower(), stripos(), sort() with SORT_STRING | SORT_FLAG_CASE # and related functions to ASCII case conversion, but substr_compare() with $case_insensitive = true and ksort()/krsort() with SORT_STRING | SORT_FLAG_CASE still follow LC_CTYPE.

The output above is from PHP 8.2.35, 8.3.36, 8.4.27 and 8.5.12. PHP 8.1.34 prints the same, except that stripos() and sort() also follow the locale (false and I,h). 8.2 changed those two but not substr_compare() or ksort().

Reproducing on PHP 8.1 to 8.5 with Docker

The official php:*-cli images have no tr_TR.UTF-8 locale, so each run generates it first. With the snippet above saved as repro.php:

for v in 8.1 8.2 8.3 8.4 8.5; do
  docker run --rm -i "php:$v-cli" sh -c '
    apt-get update -qq >/dev/null &&
    apt-get install -y -qq locales >/dev/null 2>&1 &&
    localedef -i tr_TR -f UTF-8 tr_TR.UTF-8 &&
    php -v | head -n 1 &&
    php' < repro.php
done

The 8.2 UPGRADING notes say sorting with SORT_FLAG_CASE uses ASCII case conversion, and the strtolower-ascii RFC says case-insensitive substr_compare() already did.

On master, substr_compare() calls zend_binary_strncasecmp_l(), and php_array_key_compare_string_case_unstable_i(), which ksort() and krsort() use, calls zend_binary_strcasecmp_l().

Switching these two call sites to zend_binary_strncasecmp() and zend_binary_strcasecmp() would give the documented behavior.

See https://github.com/php/php-src/pull/7506.

PHP Version
PHP 8.5.12 (cli) (built: Oct 21 2026 15:53:40) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies
Operating System

Debian GNU/Linux 13 (trixie), glibc 2.41 (php:8.5-cli Docker image)

Ngôn ngữ chính
C
Star
40.7k
Fork
8.3k
Merge trung bình
17 giờ 29 phút
Pull request đã merge (30 ngày)
10

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của php/php-src

Tất cả issue của php/php-src

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.