xmlwf: `-b BYTES` rejects `XML_SetBase()` before parsing named files, omitting newline, leaking `-d` output file, breaking `-k`, and diverging from STDIN
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 72/100
Direzione di ricerca
Start with expat/xmlwf/xmlfile.c around XML_SetBase() and expat/xmlwf/xmlwf.c around allocation tracker setup and tmain() cleanup. Compare named-file and STDIN behavior using the reproducer, including read mode (-r) and output-directory mode (-d). Done when named files behave consistently with STDIN and failures return through cleanup, preserve -k behavior, and print a newline.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
Since commit 78366891 wired -b BYTES to XML_SetAllocTrackerActivationThreshold(), xmlwf lowers the allocation tracker activation threshold before XML_ProcessFile() calls XML_SetBase(parser, filename).
For named input files (both default mmap and -r read mode), XML_SetBase(parser, filename) allocates via poolCopyString(&parser->m_dtd->pool, filename) while countBytesDirect == 0, causing the Allocation Tracker to compute amplification = +inf > 100.0 and fail before a single byte of XML is parsed. expat/xmlwf/xmlfile.c:274-277 then prints <file>: out of memory to stderr without a trailing newline and calls exit(1) directly, which:
- Causes valid files that succeed on
STDIN(e.g., a 103-byte document with-b 4096) to fail withrc=1when passed as a filename. - Produces inconsistent exit status and output stream between named files (
rc=1,stderr, no\n) andSTDIN(rc=2,stdout,STDIN:1:0: out of memory\n). - Skips
fclose(),tremove(outName),XML_ParserFree(), andcleanupUserData(), leaving an orphaned partial file in-d DIRECTORYand leaking parser memory. - Aborts immediately under
-kwithout processing subsequent input files.
Version Tested
mastera623cd8f5e(expat_2.11.0)
Minimal Reproducer
# Case 1: Identical valid 103-byte document succeeds on STDIN (rc=0) but fails as a named file (rc=1)
python3 -c 'open("/tmp/valid_tag.xml","w").write("<" + "a"*100 + "/>\n")'
xmlwf -b 4096 < /tmp/valid_tag.xml; echo "STDIN rc=$?"
xmlwf -b 4096 /tmp/valid_tag.xml; echo " | file rc=$?"
xmlwf -r -b 4096 /tmp/valid_tag.xml; echo " | file -r rc=$?"
# Case 2: Missing newline, orphan partial file under -d -m, and -k ignored
mkdir -p /tmp/out_file /tmp/out_stdin
printf '<r/>\n' > /tmp/in2.xml
printf '<r/>\n' > /tmp/in2_second.xml
xmlwf -d /tmp/out_file -m -k -b 64 /tmp/in2.xml /tmp/in2_second.xml; echo " | file_k rc=$?"
ls -la /tmp/out_file/
cat /tmp/out_file/in2.xml
xmlwf -d /tmp/out_stdin -m -b 64 < /tmp/in2.xml; echo "STDIN_dm rc=$?"
ls -la /tmp/out_stdin/
Actual Output (master a623cd8f5e)
STDIN rc=0
/tmp/valid_tag.xml: out of memory | file rc=1
/tmp/valid_tag.xml: out of memory | file -r rc=1
/tmp/in2.xml: out of memory | file_k rc=1
total 12
-rw------- 1 user user 11 ... in2.xml # orphaned partial file containing "<document>\n"
<document>
STDIN:1:0: out of memory
STDIN_dm rc=2
total 8 # out_stdin/STDIN properly removed
Tracing with EXPAT_MALLOC_DEBUG=2 xmlwf -b 4096 /tmp/valid_tag.xml vs < /tmp/valid_tag.xml confirms the exact failure in XML_SetBase() (Direct = 0, amplification = inf) and zero subsequent frees:
# Named file (fails in XML_SetBase at Direct=0, exits without freeing):
expat: Allocations(...): Direct 0, allocated + 392 to 3072 ( 3072 peak), amplification inf (xmlparse.c:8623)
expat: Allocations(...): Direct 0, allocated + 1048 to 4120 ( 4120 peak), amplification inf (xmlparse.c:9396)
/tmp/valid_tag.xml: out of memory
# STDIN (succeeds with peak 5800 B, max amplification 56.31 <= 100.0):
expat: Allocations(...): Direct 0, allocated + 392 to 3072 ( 3072 peak), amplification inf (xmlparse.c:8623)
expat: Allocations(...): Direct 103, allocated + 1048 to 4120 ( 4120 peak), amplification 40.00 (xmlparse.c:9396)
...
expat: Allocations(...): Direct 104, allocated - 1128 to 0 ( 5800 peak), amplification 0.00 (xmlparse.c:1985)
Expected Behavior
XML_SetBase(parser, filename)setup inxmlwfshould not be rejected prematurely by-b BYTESatcountBytesDirect == 0, so named files andSTDINbehave consistently.- On a genuine
XML_SetBase()OOM failure inexpat/xmlwf/xmlfile.c:274-277, the error message should end with\n, andXML_ProcessFile()should returnfalsecleanly instead of callingexit(1)sotmain()closes/removes any-doutput file, freesparser/outName/userData, and honors-k.
Root Cause
expat/xmlwf/xmlwf.c:1251-1259callsXML_SetAllocTrackerActivationThreshold(parser, attackThresholdBytes)beforeXML_ProcessFile()(xmlwf.c:1401) callsXML_SetBase(parser, filename).expat/lib/xmlparse.c:2034-2038allocates viapoolCopyString(&parser->m_dtd->pool, p)only whenp != NULL, triggeringMALLOC()atcountBytesDirect == 0.expat/xmlwf/xmlfile.c:274-277:
omitsif (! XML_SetBase(parser, filename)) { ftprintf(stderr, T("%s: out of memory"), filename); exit(1); }\n(compare line 226:T("%s: out of memory\n")) and is the onlyexit(1)inxmlfile.c, bypassing cleanup and-katexpat/xmlwf/xmlwf.c:1403-1427.
Suggested Fix
- Call
XML_SetBase(parser, useStdin ? NULL : argv[i])before loweringXML_SetAllocTrackerActivationThreshold()(or configure the allocation tracker afterXML_SetBase()), so initial parser setup is accounted uniformly for named files andSTDIN. - In
expat/xmlwf/xmlfile.c:274-277, add the trailing\n(T("%s: out of memory\n")) and returnfalse(or propagate an error status back totmain()) instead of callingexit(1)directly, sofclose(),tremove(outName),XML_ParserFree(),cleanupUserData(), and-kall execute cleanly.
- Lingua principale
- C
- Stelle
- 1.4k
- Fork
- 555
- Merge medio
- 23h 25m
- PR unite (30g)
- 61
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di libexpat/libexpat
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
xmlwf: `-m -n` emits raw illegal XML 1.0 control character U+0001 (`NSSEP`) in `name` attributesAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di libexpat/libexpat
Issue simili
-
CF_CFDP_S_SendFileData computes the file checksum from the SB buffer after CFE_SB_TransmitBufferAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
389ds/389-ds-base#8251 ·
I maintainer di solito rispondono entro 1 giorno
-
bug data-types
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
ClickHouse/clickhouse-cpp#645 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100