grimfang4 / SDL_FontCache

A generic font caching C library with loading and rendering support for SDL.
MIT License
305 stars 66 forks source link

Added null check #25

Closed sago007 closed 6 years ago

sago007 commented 6 years ago

The line

*node = (FC_StringList*)malloc(sizeof(FC_StringList));

Will cause undefined behaviour if "node" is NULL. However right before it was a while loop like this:

while(node != NULL && *node != NULL)
    {
        node = &(*node)->next;
    }

Indicating that "node" could be NULL. This caused Codacy to complain.

This pull requests adds a NULL check that will prevent the program from reaching the code that would trigger an undefined behaviour.

grimfang4 commented 6 years ago

Thanks! Yeah, I can see why it'd complain. Can you move the if before the while? Then I'll happily merge it.

sago007 commented 6 years ago

I have moved the check and removed it from the while loop. I had originally placed it after the while-loop in case it could be nulled inside the loop. I realize now that it would be impossible without another UB inside the loop.

grimfang4 commented 6 years ago

Great! Thanks for helping out!