Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw Win32Exception on success for empty section in Kernel32Util#getPrivateProfileSection #1411

Merged
merged 2 commits into from Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGES.md
Expand Up @@ -8,11 +8,11 @@ Next Release (5.11.0)
Features
--------
* [#1398](https://github.com/java-native-access/jna/pull/1398): Increase `c.s.j.p.win32.Sspi#MAX_TOKEN_SIZE` on Windows 8/Server 2012 and later - [@dbwiddis](https://github.com/dbwiddis).
* [#1398](https://github.com/java-native-access/jna/pull/1403): Rebuild AIX binaries with libffi 3.4.2 (other architectures were part of 5.10) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1403](https://github.com/java-native-access/jna/pull/1403): Rebuild AIX binaries with libffi 3.4.2 (other architectures were part of 5.10) - [@matthiasblaesing](https://github.com/matthiasblaesing).

Bug Fixes
---------

* [#1411](https://github.com/java-native-access/jna/pull/1411): Do not throw `Win32Exception` on success for empty section in `Kernel32Util#getPrivateProfileSection` - [@mkarg](https://github.com/mkarg).

Release 5.10.0
==============
Expand Down
Expand Up @@ -719,6 +719,9 @@ public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcesso
return procInfoList.toArray(new SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[0]);
}

// Prevents useless heap pollution
private static final String[] EMPTY_STRING_ARRAY = new String[0];

/**
* Retrieves all the keys and values for the specified section of an initialization file.
*
Expand All @@ -739,7 +742,12 @@ public static final SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] getLogicalProcesso
public static final String[] getPrivateProfileSection(final String appName, final String fileName) {
final char buffer[] = new char[32768]; // Maximum section size according to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms724348(v=vs.85).aspx)
if (Kernel32.INSTANCE.GetPrivateProfileSection(appName, buffer, new DWORD(buffer.length), fileName).intValue() == 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
final int lastError = Kernel32.INSTANCE.GetLastError();
if (lastError == Kernel32.ERROR_SUCCESS) {
return EMPTY_STRING_ARRAY;
} else {
throw new Win32Exception(lastError);
}
}
return new String(buffer).split("\0");
}
Expand Down
Expand Up @@ -244,9 +244,18 @@ public final void testGetPrivateProfileSection() throws IOException {
final File tmp = File.createTempFile("testGetPrivateProfileSection", ".ini");
tmp.deleteOnExit();

final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
final PrintWriter writer0 = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
try {
writer0.println("[X]");
} finally {
writer0.close();
}

final String[] lines0 = Kernel32Util.getPrivateProfileSection("X", tmp.getCanonicalPath());
assertEquals(lines0.length, 0);

final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp, true)));
try {
writer.println("[X]");
writer.println("A=1");
writer.println("foo=bar");
} finally {
Expand Down