php / php/php-src

sscanf c conversion specifier

Đang mở
#19,088 11 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Extension: standard Status: Verified
Ngôn ngữ chính
C
Star
40.4k
Fork
8.2k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

Description

The following code:

<?php

$r = sscanf('     ', '%c%n', $c, $n)
and var_dump(compact('r', 'c', 'n'));

Resulted in this output:

array(3) {
  'r' =>
  int(2)
  'c' =>
  string(0) ""
  'n' =>
  int(0)
}

But I expected this output instead:

array(3) {
  'r' =>
  int(2)
  'c' =>
  string(1) " "
  'n' =>
  int(1)
}

Expectation by fscanf, scanf, sscanf - convert formatted input, cf. Revision of IEEE Std 1003.1-2008, via Open Group Base Specifications Issue 7, 2018 edition, specifically "[CX] The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2017 defers to the ISO C standard", quoting:

The following conversion specifiers are valid:

[...]

  • c
    • Matches a sequence of bytes of the number specified by the field width (1 if no field width is present in the conversion specification). No null byte is added. The normal skip over white-space characters shall be suppressed in this case.

For PHP versions older of that date, please refer to the equally older documentation for these dates.

Why is the expectation important?

The c conversion specifier is important to scan over white space characters, so that it adjoins the s conversion specifier (supported by PHP sscanf(), compare https://github.com/php/php-src/issues/11201#issuecomment-1537369396 ) and/or leaving a space character in the $format string (supported by PHP sscanf()).


https://3v4l.org/9E2d1

8.4.10

https://3v4l.org/9E2d1	9E2d1

1:	<?php
2:	
3:	$r = sscanf('     ', '%c%n', $c, $n)
4:	and var_dump(compact('r', 'c', 'n'));
5:	
output [versions: 8.4.10 | released 2025-07-03 | took 29 ms, 18.55 MiB]
	O: array(3) {
	O:   ["r"]=>
	O:   int(2)
	O:   ["c"]=>
	O:   string(0) ""
	O:   ["n"]=>
	O:   int(0)
	O: }

+eol:

https://3v4l.org/9E2d1	9E2d1

1:	<?php
2:	
3:	$r = sscanf('     ', '%c%n', $c, $n)
4:	and var_dump(compact('r', 'c', 'n'));
5:	
output [versions: 4.3.2 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.4 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.23, 8.4.1 - 8.4.10]
	O: array(3) {
	O:   ["r"]=>
	O:   int(2)
	O:   ["c"]=>
	O:   string(0) ""
	O:   ["n"]=>
	O:   int(0)
	O: }
output [versions: 7.0.0 - 7.0.3]
	O: array(3) {
	O:   ["r"]=>
	O:   int(2)
	O:   ["c"]=>
	O:   &string(0) ""
	O:   ["n"]=>
	O:   &int(0)
	O: }
output [versions: 4.3.0 - 4.3.1]
	O: array(3) {
	O:   ["r"]=>
	O:   int(2)
	O:   ["c"]=>
	O:   string(0) ""
	O:   ["n"]=>
	O:   string(0) ""
	O: }
PHP Version
PHP 8.4.10 (cli) (built: Jul  3 2025 12:41:53) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.10, Copyright (c), by Zend Technologies
    with Xdebug v3.4.4, Copyright (c) 2002-2025, by Derick Rethans
Operating System

No response

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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.

Hướng nghiên cứu

Bắt đầu với reproducer sscanf('%c%n') đã được cung cấp, tìm implementation của sscanf trong PHP và các test hiện có cho các conversion specifier. So sánh hành vi của %c với yêu cầu POSIX/ISO C được trích dẫn, sau đó thêm regression coverage cho thấy cách xử lý whitespace được mong đợi và xác minh output được báo cáo.

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

Đánh giá

Công nghệ
c, php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/100

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.