JDK-8043720 : (smartcardio) Native memory should be handled more accurately
  • Type: Bug
  • Component: security-libs
  • Sub-Component: javax.smartcardio
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2014-05-22
  • Updated: 2016-06-13
  • Resolved: 2014-05-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 JDK 9
7u76Fixed 8u20Fixed 9 b16Fixed
Description
There are a few issues with the native memory allocation in share/native/sun/security/smartcardio/pcsc.c:

1)
    readerState = calloc(readers, sizeof(SCARD_READERSTATE));
    if (readerState == NULL) {
        throwOutOfMemoryError(env, NULL);

calloc() can return NULL due to readers be zero.
In this case OOM would be confusing.

2)
    for (i = 0; i < readers; i++) {
        free((char *)readerState[i].szReader);
    }

We can get here upon an error, so readerState[i].szReader may not be initialized.

3)
    mszReaders = malloc(size);
    if (mszReaders == NULL) {
        throwOutOfMemoryError(env, NULL);
        return NULL;
    }

If size happens to be zero, we'll get a confusing OOM.

4)
    tab = (char **)malloc(cnt * sizeof(char *));
    if (tab == NULL) {
        throwOutOfMemoryError(env, NULL);
        return NULL;
    }

Again, we can get NULL from malloc, if cnt == 0.