C: when converting between C and Java strings, properly handle illegal characters

This commit is contained in:
Alexey Andreev 2019-05-27 17:39:10 +03:00
parent d818b62686
commit 1899641f63

View File

@ -283,7 +283,7 @@ size_t teavm_mbSize(char16_t* javaChars, int32_t javaCharsCount) {
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < javaCharsCount; ++i) { for (int32_t i = 0; i < javaCharsCount; ++i) {
size_t result = c16rtomb(buffer, javaChars[i], &state); size_t result = c16rtomb(buffer, javaChars[i], &state);
if (result < 0) { if (result == -1) {
break; break;
} }
sz += result; sz += result;
@ -298,7 +298,7 @@ int32_t teavm_c16Size(char* cstring, size_t count) {
size_t result = mbrtoc16(NULL, cstring, count, &state); size_t result = mbrtoc16(NULL, cstring, count, &state);
if (result == -1) { if (result == -1) {
break; break;
} else if (result >= 0) { } else if ((int) result >= 0) {
sz++; sz++;
count -= result; count -= result;
cstring += result; cstring += result;
@ -324,7 +324,11 @@ char* teavm_stringToC(void* obj) {
char* dst = result; char* dst = result;
mbstate_t state = {0}; mbstate_t state = {0};
for (int32_t i = 0; i < charArray->size; ++i) { for (int32_t i = 0; i < charArray->size; ++i) {
dst += c16rtomb(dst, javaChars[i], &state); size_t result = c16rtomb(dst, javaChars[i], &state);
if (result == -1) {
break;
}
dst += result;
} }
*dst = '\0'; *dst = '\0';
return result; return result;
@ -344,7 +348,7 @@ TeaVM_String* teavm_cToString(char* cstring) {
int32_t result = mbrtoc16(javaChars++, cstring, clen, &state); int32_t result = mbrtoc16(javaChars++, cstring, clen, &state);
if (result == -1) { if (result == -1) {
break; break;
} else if (result >= 0) { } else if ((int) result >= 0) {
clen -= result; clen -= result;
cstring += result; cstring += result;
} }