xmlwf: `-b BYTES` rejects `XML_SetBase()` before parsing named files, omitting newline, leaking `-d` output file, breaking `-k`, and diverging from STDIN

Aperta
#1,561 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c
Ambito
backend, cli

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:

  1. Causes valid files that succeed on STDIN (e.g., a 103-byte document with -b 4096) to fail with rc=1 when passed as a filename.
  2. Produces inconsistent exit status and output stream between named files (rc=1, stderr, no \n) and STDIN (rc=2, stdout, STDIN:1:0: out of memory\n).
  3. Skips fclose(), tremove(outName), XML_ParserFree(), and cleanupUserData(), leaving an orphaned partial file in -d DIRECTORY and leaking parser memory.
  4. Aborts immediately under -k without processing subsequent input files.
Version Tested
  • master a623cd8f5e (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
  1. XML_SetBase(parser, filename) setup in xmlwf should not be rejected prematurely by -b BYTES at countBytesDirect == 0, so named files and STDIN behave consistently.
  2. On a genuine XML_SetBase() OOM failure in expat/xmlwf/xmlfile.c:274-277, the error message should end with \n, and XML_ProcessFile() should return false cleanly instead of calling exit(1) so tmain() closes/removes any -d output file, frees parser/outName/userData, and honors -k.
Root Cause
  1. expat/xmlwf/xmlwf.c:1251-1259 calls XML_SetAllocTrackerActivationThreshold(parser, attackThresholdBytes) before XML_ProcessFile() (xmlwf.c:1401) calls XML_SetBase(parser, filename).
  2. expat/lib/xmlparse.c:2034-2038 allocates via poolCopyString(&parser->m_dtd->pool, p) only when p != NULL, triggering MALLOC() at countBytesDirect == 0.
  3. expat/xmlwf/xmlfile.c:274-277:
    if (! XML_SetBase(parser, filename)) {
      ftprintf(stderr, T("%s: out of memory"), filename);
      exit(1);
    }
    
    omits \n (compare line 226: T("%s: out of memory\n")) and is the only exit(1) in xmlfile.c, bypassing cleanup and -k at expat/xmlwf/xmlwf.c:1403-1427.
Suggested Fix
  1. Call XML_SetBase(parser, useStdin ? NULL : argv[i]) before lowering XML_SetAllocTrackerActivationThreshold() (or configure the allocation tracker after XML_SetBase()), so initial parser setup is accounted uniformly for named files and STDIN.
  2. In expat/xmlwf/xmlfile.c:274-277, add the trailing \n (T("%s: out of memory\n")) and return false (or propagate an error status back to tmain()) instead of calling exit(1) directly, so fclose(), tremove(outName), XML_ParserFree(), cleanupUserData(), and -k all execute cleanly.
Lingua principale
C
Stelle
1.4k
Fork
555
Merge medio
23h 25m
PR unite (30g)
61

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di libexpat/libexpat

Tutte le issue di libexpat/libexpat

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.