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

JDK-8260432: allocateSpaceForGP in freetypeScaler.c might leak memory #2250

Closed
wants to merge 3 commits into from

Conversation

MBaesken
Copy link
Member

@MBaesken MBaesken commented Jan 27, 2021

The function AllocateSpaceForGP in freetypeScaler.c calls potentially 2 times malloc ; however the memory is not always freed correctly in case of errors.
See also the related sonar issue :
https://sonarcloud.io/project/issues?id=shipilev_jdk&languages=c&open=AXck8B_SBBG2CXpcngxr&resolved=false&severities=BLOCKER&types=BUG


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8260432: allocateSpaceForGP in freetypeScaler.c might leak memory

Reviewers

Download

$ git fetch https://git.openjdk.java.net/jdk pull/2250/head:pull/2250
$ git checkout pull/2250

@bridgekeeper
Copy link

bridgekeeper bot commented Jan 27, 2021

👋 Welcome back mbaesken! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jan 27, 2021
@openjdk
Copy link

openjdk bot commented Jan 27, 2021

@MBaesken The following label will be automatically applied to this pull request:

  • 2d

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the 2d client-libs-dev@openjdk.org label Jan 27, 2021
@mlbridge
Copy link

mlbridge bot commented Jan 27, 2021

Webrevs

@@ -1285,7 +1287,7 @@ static int allocateSpaceForGP(GPData* gpdata, int npoints, int ncontours) {
}

/* failure if any of mallocs failed */
if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL)
if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL)
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be cleaner to free the remaining allocations on the failing path:

if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) {
   if (gpdata->pointTypes != NULL)  free(gpdata->pointTypes);
   if (gpdata->pointCoords != NULL) free(gpdata->pointCoords);
   return 0;
} else {
   return 1;
}

Copy link
Member Author

@MBaesken MBaesken Jan 27, 2021

Choose a reason for hiding this comment

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

Then we would free as well for the realloc code path
} else {
/* do we have enough space? */
...
}
Is this okay?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think that is better: realloc can fail as well, and we don't want to leak those?

Copy link
Member

Choose a reason for hiding this comment

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

Please also reset the free'd pointers to NULL in GPData* before returning.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hello Thomas and Aleksey, I moved the free calls and added the NULL settings. Are you fine with the latest commit?
Thanks, Matthias

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

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

This looks fine to me, modulo stylistic nits

@@ -1285,10 +1285,12 @@ static int allocateSpaceForGP(GPData* gpdata, int npoints, int ncontours) {
}

/* failure if any of mallocs failed */
if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL)
if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) {
if (gpdata->pointTypes != NULL) { free(gpdata->pointTypes); gpdata->pointTypes = NULL; }
Copy link
Member

Choose a reason for hiding this comment

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

You might want to add an extra space before gpdata->pointTypes = NULL; to align the statements vertically. You call.

Copy link
Member

Choose a reason for hiding this comment

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

Can we also style it consistently, like:

  if (gpdata->pointTypes != NULL)  { 
      free(gpdata->pointTypes);
      gpdata->pointTypes = NULL;
  }
  if (gpdata->pointCoords != NULL) {
      free(gpdata->pointCoords);
      gpdata->pointCoords = NULL;
  }

@shipilev
Copy link
Member

Pull from master to get x86_32 GHA jobs fixed and running.

@openjdk
Copy link

openjdk bot commented Jan 28, 2021

@MBaesken This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8260432: allocateSpaceForGP in freetypeScaler.c might leak memory

Reviewed-by: shade, stuefe

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 23 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jan 28, 2021
Copy link
Member

@tstuefe tstuefe left a comment

Choose a reason for hiding this comment

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

Hi Matthias,

looks fine. See remark below, but that is just nitpicking, this is fine as it is.

Cheers, Thomas

if (gpdata->pointCoords != NULL) {
free(gpdata->pointCoords);
gpdata->pointCoords = NULL;
}
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

If you wanted, you could streamline this to:

    if (gpdata->pointTypes == NULL || gpdata->pointCoords == NULL) {
        free(gpdata->pointTypes);
        free(gpdata->pointCoords);
        gpdata->pointTypes = gpdata->pointCoords = NULL;
        return 0;	

Since free(NULL) is valid and a noop.

@MBaesken
Copy link
Member Author

/integrate

@openjdk openjdk bot closed this Jan 28, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Jan 28, 2021
@openjdk
Copy link

openjdk bot commented Jan 28, 2021

@MBaesken Since your change was applied there have been 35 commits pushed to the master branch:

  • abc4300: 8257746: Regression introduced with JDK-8250984 - memory might be null in some machines
  • 13ca433: 8259628: jdk/net/ExtendedSocketOption/AsynchronousSocketChannelNAPITest.java fails intermittently
  • baf46ba: 8259801: Enable XML Signature secure validation mode by default
  • 20e7df5: 8260466: Test TestHeapDumpOnOutOfMemoryError.java needs multiple @test sections
  • 11d6467: 8260407: cmp != __null && cmp->Opcode() == Op_CmpL failure with -XX:StressLongCountedLoop=200000000 in lucene
  • d07af2b: 8255531: MethodHandles::permuteArguments throws NPE when duplicating dropped arguments
  • a68c6c2: 8260579: PPC64 and S390 builds failures after JDK-8260467
  • 8752257: 8260502: [s390] NativeMovRegMem::verify() fails because it's too strict
  • 8fe1323: 8260520: Avoid getting permissions in JarFileFactory when no SecurityManager installed
  • ecde52e: 8260506: VersionHelper cleanup
  • ... and 25 more: https://git.openjdk.java.net/jdk/compare/fd2641ed36c8cdb59c33675bd9ed909b47cd002e...master

Your commit was automatically rebased without conflicts.

Pushed as commit 3aabbd7.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2d client-libs-dev@openjdk.org integrated Pull request has been integrated
3 participants