Skip to content

Commit

Permalink
8312612: handle WideCharToMultiByte return values
Browse files Browse the repository at this point in the history
Reviewed-by: clanger
  • Loading branch information
MBaesken committed Jul 28, 2023
1 parent 34173ff commit d9559f9
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
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
@@ -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
@@ -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

5 comments on commit d9559f9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk21u

@openjdk
Copy link

@openjdk openjdk bot commented on d9559f9 Sep 21, 2023

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-d9559f9b in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d9559f9b from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 28 Jul 2023 and was reviewed by Christoph Langer.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:

$ git fetch https://github.com/openjdk-bots/jdk21u.git MBaesken-backport-d9559f9b:MBaesken-backport-d9559f9b
$ git checkout MBaesken-backport-d9559f9b
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git MBaesken-backport-d9559f9b

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on d9559f9 Oct 27, 2023

Choose a reason for hiding this comment

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

@MBaesken the backport was successfully created on the branch MBaesken-backport-d9559f9b in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit d9559f9b from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 28 Jul 2023 and was reviewed by Christoph Langer.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-d9559f9b:MBaesken-backport-d9559f9b
$ git checkout MBaesken-backport-d9559f9b
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-d9559f9b

Please sign in to comment.