fileinfo: built-in magic database php_magic_database is not aligned, breaks on strict-alignment CPUs (ARMv5, 32-bit)
維護者通常 1 天內回覆
評估
研究方向
從 ext/fileinfo/create_data_file.php 和為 php_magic_database 產生的 ext/fileinfo/data_file.c 宣告開始。檢查 ZEND_SET_ALIGNED 如何用於其他宣告,接著重新產生資料檔並驗證其對齊情況。內建 magic 資料庫完成對齊,且 finfo_open() 和 mime_content_type() 能在嚴格對齊的 ARM 組建中正常運作時,即視為完成。
由索引模型根據 Issue 內容生成。
描述
Description
The following code:
<?php
var_dump(mime_content_type('/tmp/file.zip'));
Resulted in this output:
ValueError: Path must not be empty.
But I expected this output instead:
application/zip
Description:
On ARMv5 (32-bit cpu), built with GCC and -Os, finfo_open() and mime_content_type() fail in PHP 8.5.x with:
ValueError: Path must not be empty.
Cause:
php_magic_database (ext/fileinfo/data_file.c) is declared as const unsigned char[][], so the compiler only guarantees 1-byte alignment.
libmagic apprentice_map() casts the buffer to uint32_t * to check MAGICNO/VERSIONNO, then uses it as an array of struct magic (8-byte aligned).
On CPUs that don't support unaligned access, the header reads back wrong and the built-in database is rejected.
libmagic then falls back to apprentice_load(NULL), which calls the stream layer with an empty path and throws the ValueError.
Whether the bug appears depends on where the linker places the symbol (nm shows an address that isn't a multiple of 4 or 8). GCC on ARM normally word-aligns large arrays, but not with -Os.
Fix: declare the array with ZEND_SET_ALIGNED(8, ...) in create_data_file.php and regenerate data_file.c
Since ext/fileinfo/create_data_file.php is generated I will not provide a patch for it, but this is the AI generated proposal:
-echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] ZEND_NONSTRING = {\n";
+/* libmagic accesses the database as uint32_t and struct magic, which need 8-byte alignment */
+echo 'ZEND_SET_ALIGNED(8, const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "]) ZEND_NONSTRING = {\n";
My own patch to solve my problem: directly in data_file.c
--- php-8.5.12/ext/fileinfo/data_file.c 2026-10-28 16:30:57.011450103 +0100
+++ php-8.5.12.modified/ext/fileinfo/data_file.c 2026-10-28 16:22:31.851759910 +0100
@@ -3 +3 @@
-const unsigned char php_magic_database[10113][1024] ZEND_NONSTRING = {
+const unsigned char php_magic_database[10113][1024] ZEND_NONSTRING attribute((aligned(8))) = {
PHP Version
PHP 8.5.12 (cli) (built: Oct 28 2026 16:52:49) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.12, Copyright (c) Zend Technologies
with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies
Operating System
No response
- 主要語言
- C
- 星號
- 41.7k
- 分支
- 8.5k
- 平均合併
- 2 天 10 小時
- 30 天內合併 PR
- 158
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
php/php-src 的其他 Issue
-
Windows CLI: get_ps_title() passes bytes to GetConsoleTitleW() instead of characters, overflowing the 260-wchar_t buffer可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
master: part of the mbstring bailout fix got lost in a merge可能已有人在做 @lazerg 於 8 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 64/100
維護者通常 1 天內回覆
-
quoted_printable_encode() produces lines longer than 76 characters for bytes 0xF5 to 0xFF可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 2/5 1-3 小時 新手友好度 76/100
維護者通常 1 天內回覆
-
Bug Status: Needs Triage
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
cyrusimap/cyrus-imapd#6640 ·
維護者通常 2 天內回覆
-
難度 2/5 1-3 小時 新手友好度 60/100
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 68/100
GalusPeres/HomeTiles#139 · 1 則留言 ·
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
DaveGamble/cJSON#1122 ·
維護者通常 12 天內回覆
-
Type: Bug
難度 2/5 1-3 小時 新手友好度 76/100
apache/nuttx-apps#3942 ·
維護者通常 1 天內回覆