bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
689 stars 70 forks source link

Decode error in _CoreAudio.CFString_to_str #151

Closed KoharuYuzuki closed 2 years ago

KoharuYuzuki commented 2 years ago

First of all I apologize that I am not good at English.

In the following environment, attempting to print() the result of soundcard.all_speakers() or soundcard.all_microphones() may result in a decoding error.

=== environment === OS: macOS 12.3.1 Chip: M1 Memory: 8GB

=== code === import soundcard speakers = soundcard.all_speakers() print(speakers)

=== error === File "/.../soundcard/coreaudio.py", line 134, in name return _CoreAudio.CFString_to_str(name) File "/.../soundcard/coreaudio.py", line 297, in CFString_to_str assert err == 1, "Could not decode string"

The cause of the error appears to be insufficient buffer size when trying to decode characters such as Japanese. As a test, I multiplied str_length by 4, and the error no longer occurred.

This issue may be helpful. https://github.com/curl/curl/issues/1823

Thank you.

bastibe commented 2 years ago

Thank you for reporting and analysing this issue!

UTF-8 should never use more than four bytes per character, so a four-times overcommit should always work. Would you like to contribute the change as a pull request?

KoharuYuzuki commented 2 years ago

Yes, I have created a pull request.