Skip to content

Commit e831f09

Browse files
ntaufleisch
authored andcommitted
Fix Unicode property keys in C bindings
The C bindings would convert a char* to String using the default constructor, which uses the Latin1 encoding, breaking when a key contains a Unicode character (e.g. an ID3v2 comment description).
1 parent 7d86716 commit e831f09

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

bindings/c/tag_c.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,13 @@ void _taglib_property_set(TagLib_File *file, const char* prop, const char* value
469469
return;
470470

471471
auto tfile = reinterpret_cast<FileRef *>(file);
472+
auto propStr = charArrayToString(prop);
472473
PropertyMap map = tfile->tag()->properties();
473474

474475
if(value) {
475-
auto property = map.find(prop);
476+
auto property = map.find(propStr);
476477
if(property == map.end()) {
477-
map.insert(prop, StringList(charArrayToString(value)));
478+
map.insert(propStr, StringList(charArrayToString(value)));
478479
}
479480
else {
480481
if(append) {
@@ -486,7 +487,7 @@ void _taglib_property_set(TagLib_File *file, const char* prop, const char* value
486487
}
487488
}
488489
else {
489-
map.erase(prop);
490+
map.erase(propStr);
490491
}
491492

492493
tfile->setProperties(map);
@@ -531,7 +532,7 @@ char **taglib_property_get(const TagLib_File *file, const char *prop)
531532

532533
const PropertyMap map = reinterpret_cast<const FileRef *>(file)->properties();
533534

534-
auto property = map.find(prop);
535+
auto property = map.find(charArrayToString(prop));
535536
if(property == map.end())
536537
return NULL;
537538

@@ -573,16 +574,17 @@ bool _taglib_complex_property_set(
573574
return false;
574575

575576
auto tfile = reinterpret_cast<FileRef *>(file);
577+
auto keyStr = charArrayToString(key);
576578

577579
if(value == NULL) {
578-
return tfile->setComplexProperties(key, {});
580+
return tfile->setComplexProperties(keyStr, {});
579581
}
580582

581583
VariantMap map;
582584
const TagLib_Complex_Property_Attribute** attrPtr = value;
583585
while(*attrPtr) {
584586
const TagLib_Complex_Property_Attribute *attr = *attrPtr;
585-
String attrKey(attr->key);
587+
auto attrKey = charArrayToString(attr->key);
586588
switch(attr->value.type) {
587589
case TagLib_Variant_Void:
588590
map.insert(attrKey, Variant());
@@ -627,8 +629,8 @@ bool _taglib_complex_property_set(
627629
++attrPtr;
628630
}
629631

630-
return append ? tfile->setComplexProperties(key, tfile->complexProperties(key).append(map))
631-
: tfile->setComplexProperties(key, {map});
632+
return append ? tfile->setComplexProperties(keyStr, tfile->complexProperties(keyStr).append(map))
633+
: tfile->setComplexProperties(keyStr, {map});
632634
}
633635

634636
} // namespace
@@ -676,7 +678,7 @@ TagLib_Complex_Property_Attribute*** taglib_complex_property_get(
676678
return NULL;
677679
}
678680

679-
const auto variantMaps = reinterpret_cast<const FileRef *>(file)->complexProperties(key);
681+
const auto variantMaps = reinterpret_cast<const FileRef *>(file)->complexProperties(charArrayToString(key));
680682
if(variantMaps.isEmpty()) {
681683
return NULL;
682684
}

tests/test_tag_c.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class TestTagC : public CppUnit::TestFixture
110110
taglib_property_set(file, "COMPOSER", "Composer 1");
111111
taglib_property_set_append(file, "COMPOSER", "Composer 2");
112112
taglib_property_set(file, "ALBUMARTIST", "Album Artist");
113+
taglib_property_set(file, "COMMENT:\xE2\x80\xBB", "Comment (with description)");
113114

114115
// cppcheck-suppress cstyleCast
115116
TAGLIB_COMPLEX_PROPERTY_PICTURE(props, "JPEG Data", 9, "Written by TagLib",
@@ -141,6 +142,7 @@ class TestTagC : public CppUnit::TestFixture
141142
{"DATE"s, {"2023"s}},
142143
{"COMPOSER"s, {"Composer 1"s, "Composer 2"s}},
143144
{"COMMENT"s, {"Comment"s}},
145+
{"COMMENT:\xE2\x80\xBB"s, {"Comment (with description)"s}},
144146
{"ARTIST"s, {"Artist"s}},
145147
{"ALBUMARTIST"s, {"Album Artist"s}},
146148
{"ALBUM"s, {"Album"s}}

0 commit comments

Comments
 (0)