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

fix: Prevent segmentation fault at Simple generic Stack #1463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Artursrossi
Copy link

Description of Change

  • Prevent segmentation fault error at stack.c grow function
    • After malloc, there is an iteration without any check if malloc returned null

References

Checklist

  • Added description of change
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: Used realloc instead of malloc

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

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

In principle this is a change in a very good direction: standard functions are always better than manual work.

free(array);
array = tmp;
array = (void **)realloc(array, sizeof(void *) * max);
assert(array); /* tests whether pointer is assigned to memory. */
Copy link
Member

Choose a reason for hiding this comment

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

If asserts are disabled and realloc would return NULL, there will be a memory leak.

C++ reference on realloc states

If there is not enough memory, the old memory block is not freed and null pointer is returned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants