8312612: handle WideCharToMultiByte return values

Reviewed-by: clanger
This commit is contained in:
Matthias Baesken
2023-07-28 13:45:19 +00:00
parent 34173ff0d1
commit d9559f9b24
3 changed files with 32 additions and 13 deletions

View File

@@ -1790,10 +1790,16 @@ void CCombinedSegTable::GetEUDCFileName(LPWSTR lpszFileName, int cchFileName)
DWORD dwBytes = sizeof(szFileName);
// try Typeface-specific EUDC font
char szTmpName[80];
VERIFY(::WideCharToMultiByte(CP_ACP, 0, szFamilyName, -1,
szTmpName, sizeof(szTmpName), NULL, NULL));
LONG lStatus = ::RegQueryValueExA(hKey, (LPCSTR) szTmpName,
NULL, &dwType, szFileName, &dwBytes);
int nb = ::WideCharToMultiByte(CP_ACP, 0, szFamilyName, -1,
szTmpName, sizeof(szTmpName), NULL, NULL);
VERIFY(nb);
LONG lStatus = 1;
if (nb > 0) {
lStatus = ::RegQueryValueExA(hKey, (LPCSTR) szTmpName,
NULL, &dwType, szFileName, &dwBytes);
}
BOOL fUseDefault = FALSE;
if (lStatus != ERROR_SUCCESS){ // try System default EUDC font
if (m_fTTEUDCFileExist == FALSE)

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -3925,9 +3925,13 @@ static void throwPrinterException(JNIEnv *env, DWORD err) {
sizeof(t_errStr),
NULL );
WideCharToMultiByte(CP_UTF8, 0, t_errStr, -1,
int nb = WideCharToMultiByte(CP_UTF8, 0, t_errStr, -1,
errStr, sizeof(errStr), NULL, NULL);
JNU_ThrowByName(env, PRINTEREXCEPTION_STR, errStr);
if (nb > 0) {
JNU_ThrowByName(env, PRINTEREXCEPTION_STR, errStr);
} else {
JNU_ThrowByName(env, PRINTEREXCEPTION_STR, "secondary error during OS message extraction");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -35,16 +35,25 @@ LPSTR UnicodeToUTF8(const LPCWSTR lpUnicodeStr)
{
DWORD dwUTF8Len = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, nullptr, 0, nullptr, nullptr);
LPSTR lpUTF8Str = new CHAR[dwUTF8Len];
if (lpUTF8Str == NULL) return NULL;
memset(lpUTF8Str, 0, sizeof(CHAR) * (dwUTF8Len));
WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, lpUTF8Str, dwUTF8Len, nullptr, nullptr);
return lpUTF8Str;
int nb = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, lpUTF8Str, dwUTF8Len, nullptr, nullptr);
if (nb > 0) {
return lpUTF8Str;
}
delete[] lpUTF8Str;
return NULL;
}
void UnicodeToUTF8AndCopy(LPSTR dest, LPCWSTR src, SIZE_T maxLength) {
LPSTR utf8EncodedName = UnicodeToUTF8(src);
strncpy(dest, utf8EncodedName, maxLength - 1);
delete[] utf8EncodedName;
dest[maxLength - 1] = '\0';
if (utf8EncodedName != NULL) {
strncpy(dest, utf8EncodedName, maxLength - 1);
delete[] utf8EncodedName;
dest[maxLength - 1] = '\0';
} else {
if (maxLength > 0) dest[0] = '\0';
}
}
#ifdef __cplusplus