Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Use of realloc may cause data corruption #3

Open
cversek opened this issue Aug 27, 2013 · 0 comments
Open

Use of realloc may cause data corruption #3

cversek opened this issue Aug 27, 2013 · 0 comments

Comments

@cversek
Copy link

cversek commented Aug 27, 2013

I've been tracing a problem with the command strings frequently being corrupted after using addCommand several times. I believe that the following line in SerialCommand.cpp is a bug:

   commandList = (SerialCommandCallback *) realloc(commandList, (commandCount + 1) * sizeof(SerialCommandCallback));

The C++ reference on realloc states the following:

The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. If the new size is larger, the value of the newly allocated portion is indeterminate.

A simple but inelegant fix would be to hard-code the size of commandList in the header file and limit the maximum number of commands.

Perhaps a more flexible dynamic allocation approach could follow from a linked-list implementation.

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

No branches or pull requests

1 participant