Skip to content

Commit

Permalink
8312612: handle WideCharToMultiByte return values
Browse files Browse the repository at this point in the history
Backport-of: d9559f9b24ee76c074cefcaf256d11ef5a7cc5b7
  • Loading branch information
MBaesken committed Nov 2, 2023
1 parent 57bf412 commit 8922e52
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
14 changes: 10 additions & 4 deletions src/java.desktop/windows/native/libawt/windows/awt_Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions src/java.desktop/windows/native/libawt/windows/awt_PrintJob.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
}
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

1 comment on commit 8922e52

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.