Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

fileinfo: built-in magic database php_magic_database is not aligned, breaks on strict-alignment CPUs (ARMv5, 32-bit)

Aberta Para iniciantes
#25,029 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

@lazerg já está trabalhando nisso.

Desde 29/10/2026.

  • #25030 de @lazerg — aberto

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
73/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
c
Domínio
backend

Direção de pesquisa

Comece por ext/fileinfo/create_data_file.php e pela declaração gerada ext/fileinfo/data_file.c para php_magic_database. Verifique como ZEND_SET_ALIGNED é usado em outras declarações, depois gere novamente o arquivo de dados e confira seu alinhamento. O trabalho estará concluído quando o banco de dados magic integrado estiver alinhado e finfo_open() e mime_content_type() funcionarem em builds ARM com alinhamento estrito.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Bug Extension: fileinfo Status: Verified
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

Linguagem predominante
C
Estrelas
41.7k
Forks
8.5k
Merge médio
2d 10h
PRs com merge (30d)
158

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de php/php-src

Todas as issues de php/php-src

Issues semelhantes

Mais issues de C

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.