Skip to content

GPX/GGZ export: invalid control characters and doubled line breaks (CR CR LF) in the file #1329

Closed

Description

@nagisml

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

  1. Have a cache whose long description contains \x02, and a log with line 1\r\nline 2 (any GSAK-imported cache with a multi-line log).
  2. Export it as GPX (on Windows for the second problem).
  3. Parse the file: python -c "import xml.etree.ElementTree as ET; ET.parse('export.gpx')" fails with not well-formed (invalid token).
  4. 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 both write_export() and the MTP path, so Python doesn't translate line endings. Optionally also turn \r\n into \n in the written text, so the output is the same on all platforms.

Regression tests:

  • Export a cache with \x02 in 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\n through the real file writer, and check that the bytes contain no \r\r\n.

Environment

No response

Activity

  1. added a commit that references this issue on Nov 18, 2026
    bf95b5f
  2. github-actions commented on Nov 18, 2026

    @github-actions
    Contributor

    @nagisml This was fixed in PR #1330 and will be included in the next beta release. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions