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

integer overflow in malloc() #1082

Closed
setharnold opened this issue Feb 21, 2018 · 2 comments
Closed

integer overflow in malloc() #1082

setharnold opened this issue Feb 21, 2018 · 2 comments
Labels
removed_components Issues that affect MJ2, JPWL or JP3D wontfix

Comments

@setharnold
Copy link
Contributor

parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));

            parameters->tcp_numlayers = numlayers;
            matrix_width = parameters->numresolution[0] + parameters->numresolution[1] + parameters->numresolution[2];
            parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
            s = s + 2;

Hello, note the malloc() argument may suffer from an integer multiplication overflow if these parameters are not bounded elsewhere in the code. I'd like to suggest that all such malloc(a*b) calls check their parameters for overflow, or switch to calloc(), as appropriate. (With three inputs, this one may take more work than usual.)

Thanks

@stweil
Copy link
Contributor

stweil commented Feb 26, 2018

parameters->numresolution is [3, 3, 1], so matrix_width is 7. Therefore limiting numlayers would be sufficient here. What is a reasonable limit for that value?

The missing check for the result of malloc being NULL is more important.

@setharnold
Copy link
Contributor Author

If matrix_width is always 7 then this is an easy fix:

  • clean up and return an error if matrix_width != 7
  • use calloc(numlayers, matrix_width * sizeof(int))

Thanks

@rouault rouault closed this as completed May 6, 2021
@rouault rouault added wontfix removed_components Issues that affect MJ2, JPWL or JP3D labels May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
removed_components Issues that affect MJ2, JPWL or JP3D wontfix
Projects
None yet
Development

No branches or pull requests

3 participants