Skip to content

Commit

Permalink
Simplify freeing of buffer in UTF8_REJECT cases
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterDuke17 committed Sep 8, 2019
1 parent e0e3ea9 commit ab86372
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/strings/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,21 @@ MVMuint32 MVM_string_utf8_decodestream(MVMThreadContext *tc, MVMDecodeStream *ds
break;
}
case UTF8_REJECT:
MVMGrapheme32 *waste[] = { buffer, NULL };
if (bufsize >= 3) {
MVMGrapheme32 a = buffer[pos - 2], b = buffer[pos - 1], c = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near bytes %02x %02x %02x", a, b, c);
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8 near bytes %02x %02x %02x", a, b, c);
}
else if (bufsize == 2) {
MVMGrapheme32 a = buffer[pos - 1], b = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near bytes %02x %02x", a, b);
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8 near bytes %02x %02x", a, b);
}
else if (bufsize == 1) {
MVMGrapheme32 a = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near byte %02x", a);
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8 near byte %02x", a);
}
else {
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8");
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8");
}
break;
}
Expand Down Expand Up @@ -450,24 +447,21 @@ MVMuint32 MVM_string_utf8_decodestream(MVMThreadContext *tc, MVMDecodeStream *ds
break;
}
case UTF8_REJECT:
MVMGrapheme32 *waste[] = { buffer, NULL };
if (bufsize >= 3) {
MVMGrapheme32 a = buffer[pos - 2], b = buffer[pos - 1], c = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near bytes %02x %02x %02x", a, b, c);
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8 near bytes %02x %02x %02x", a, b, c);
}
else if (bufsize == 2) {
MVMGrapheme32 a = buffer[pos - 1], b = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near bytes %02x %02x", a, b);
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8 near bytes %02x %02x", a, b);
}
else if (bufsize == 1) {
MVMGrapheme32 a = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near byte %02x", a);
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8 near byte %02x", a);
}
else {
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8");
MVM_exception_throw_adhoc_free(tc, waste, "Malformed UTF-8");
}
break;
}
Expand Down Expand Up @@ -521,24 +515,21 @@ MVMuint32 MVM_string_utf8_decodestream(MVMThreadContext *tc, MVMDecodeStream *ds
break;
}
case UTF8_REJECT:
MVMGrapheme32 *waste[] = { buffer, NULL };
if (bufsize >= 3) {
MVMGrapheme32 a = buffer[pos - 2], b = buffer[pos - 1], c = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near bytes %02x %02x %02x", a, b, c);
MVM_exception_throw_adhoc(tc, waste, "Malformed UTF-8 near bytes %02x %02x %02x", a, b, c);
}
else if (bufsize == 2) {
MVMGrapheme32 a = buffer[pos - 1], b = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near bytes %02x %02x", a, b);
MVM_exception_throw_adhoc(tc, waste, "Malformed UTF-8 near bytes %02x %02x", a, b);
}
else if (bufsize == 1) {
MVMGrapheme32 a = buffer[pos];
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8 near byte %02x", a);
MVM_exception_throw_adhoc(tc, waste, "Malformed UTF-8 near byte %02x", a);
}
else {
MVM_free(buffer);
MVM_exception_throw_adhoc(tc, "Malformed UTF-8");
MVM_exception_throw_adhoc(tc, waste, "Malformed UTF-8");
}
break;
}
Expand Down

0 comments on commit ab86372

Please sign in to comment.