Skip to content

Fix UnicodeEncodeError in csv adapter#229

Merged
yunzheng merged 1 commit into
fox-it:mainfrom
JazzCore:csv-surrogates
Jun 10, 2026
Merged

Fix UnicodeEncodeError in csv adapter#229
yunzheng merged 1 commit into
fox-it:mainfrom
JazzCore:csv-surrogates

Conversation

@JazzCore

@JazzCore JazzCore commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

I was getting UnicodeEncodeError errors for multiple dissect plugins (shimcache, different MRU ones) when using rdump export to CSV with these exceptions:

Traceback (most recent call last):
  File "/home/user/venv/bin/rdump", line 6, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/user/venv/lib/python3.12/site-packages/flow/record/utils.py", line 95, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/venv/lib/python3.12/site-packages/flow/record/tools/rdump.py", line 427, in main
    print_error(e)
  File "/home/user/venv/lib/python3.12/site-packages/flow/record/tools/rdump.py", line 423, in main
    record_writer.write(rec)
  File "/home/user/venv/lib/python3.12/site-packages/flow/record/adapter/csvfile.py", line 77, in write
    self.writer.writerow(rdict)
  File "/usr/lib/python3.12/csv.py", line 164, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 79-83: surrogates not allowed

There are 2 parts of this issue - more general one when python csv lib calls encode on any record fields with string type with unicode surrogates, and another one in some plugins (e.g. shimcache) where path record field have windows_path type. This type of issue with surrogates was partially fixed in #181, but looks like python csv lib calls str() instead of repr(), so surrogate replacement code was never triggered.

These can happen if record fields contain unicode surrogates when
exporting to csv files.
@Schamper Schamper requested a review from yunzheng June 10, 2026 13:39
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.29%. Comparing base (348ce96) to head (1f58900).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #229      +/-   ##
==========================================
+ Coverage   84.25%   84.29%   +0.03%     
==========================================
  Files          35       35              
  Lines        3748     3756       +8     
==========================================
+ Hits         3158     3166       +8     
  Misses        590      590              
Flag Coverage Δ
unittests 84.29% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yunzheng yunzheng left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@yunzheng yunzheng merged commit e6b58fd into fox-it:main Jun 10, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants