Repository navigation
Description
Description
Problem
The GPX/GGZ file export writes text from the database without cleaning it. That causes two problems, both seen in real exports compared with GSAK's export of the same data:
1. Control characters make the file invalid XML
Text is written as stored, including control characters that XML 1.0 doesn't allow (U+0000–U+001F except tab, LF and CR). One such character makes the whole file invalid XML. A strict parser (most XML libraries) then rejects the file or stops reading at that point.
2. On Windows every line break is written twice (\r\r\n)
On Windows the export writes \r\r\n wherever the stored text has a line break (\r\n). XML parsers turn \r\r\n into two line breaks. Every multi-line log, user note and plain-text description therefore gets an empty line between its lines in the app that reads the file.
Cause: write_export() in src/opensak/gps/garmin.py opens the output file in text mode:
open(part, "w", encoding="utf-8")
On Windows, text mode turns every \n into \r\n, including the \n of a \r\n that is already in the text. Text from GSAK databases and geocaching.com uses \r\n, so it becomes \r\r\n. The Garmin MTP path in the same file (open(tmp_path, "w", encoding="utf-8")) has the same problem. Linux and macOS are not affected.
Steps to Reproduce
Steps to reproduce
- Have a cache whose long description contains
\x02, and a log withline 1\r\nline 2(any GSAK-imported cache with a multi-line log). - Export it as GPX (on Windows for the second problem).
- Parse the file:
python -c "import xml.etree.ElementTree as ET; ET.parse('export.gpx')"fails withnot well-formed (invalid token). - Without the
\x02, count the raw bytes:open('export.gpx','rb').read().count(b'\r\r\n')is > 0.
Expected Behavior
Expected: the file parses, the invalid character is left out, and one line break stays one line break.
Actual: a parse error, and line 1\r\r\nline 2, which parsers read as two line breaks.
Proposed fix
- In the GPX/GGZ writer, remove the characters that XML 1.0 doesn't allow from all text and attribute values (descriptions, hints, logs, user notes, names, travel bug names, custom data), in one place. That covers every source (GSAK import, GPX import, API, manual edits).
- Open the output file with
newline=""(or in binary mode) in bothwrite_export()and the MTP path, so Python doesn't translate line endings. Optionally also turn\r\ninto\nin the written text, so the output is the same on all platforms.
Regression tests:
- Export a cache with
\x02in the description and in a log, and check that the result parses and the text is there without the character. - Export a cache whose description, log and user note contain
\r\nthrough the real file writer, and check that the bytes contain no\r\r\n.
Environment
No response
Activity
Leave out invalid XML characters and write single line breaks in GPX/…
github-actions commentedon Nov 18, 2026
@nagisml This was fixed in PR #1330 and will be included in the next beta release. 🎉