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

protect Config #13

Closed
wants to merge 3 commits into from
Closed

protect Config #13

wants to merge 3 commits into from

Conversation

elral
Copy link
Collaborator

@elral elral commented Sep 6, 2021

Small change but a longer explanation:

Within readConfig() readBuffer[MEM_LEN_CONFIG + 1] is defined and a RAM area is reserved. Then the config (String cfg) is copied to readBuffer[].
In the nexr steps readBuffer[] is processed, all "." and ":" are exchanged to NULL (string termination) and for each device pointers are defined which are pointing to this reserved RAM area.
At the end of the function readBuffer[] this RAM area is released, but the pointer are sstill pointing to this RAM area.
In the following program it can happen, that additional RAM is required and that the remaining RAM area is not sufficient. In this case the released RAM area from readConfig[] is used and the config is mixed up.

Defining readBuffer[MEM_LEN_CONFIG + 1] globally will avoid that this RAM area will be used again.

I have an other idea not to double the config but still to use the original buffer. This would save additional 256 Bytes of RAM. Most is working but I need some more time to finish this. But for now I would stay with this change.

/edit: 256 Bytes for the ProMicro which is already short of memory

Fixes #14

@neilenns neilenns linked an issue Sep 6, 2021 that may be closed by this pull request
Copy link
Contributor

@neilenns neilenns left a comment

Choose a reason for hiding this comment

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

Nice catch. Just need Doc to review this too!

@MobiFlight-Admin
Copy link
Collaborator

If it is inside the method, my understanding is that the memory is allocated on the stack and on leaving the method the memory is freed.
Where do you see pointers pointing to the memory area?

@elral
Copy link
Collaborator Author

elral commented Sep 6, 2021

Mmmhhhmmm,
maybe I have a complete misunderstanding...
char readBuffer[MEM_LEN_CONFIG + 1] = "";
reserves a memory area for readBuffer[]. With
cfg.toCharArray(readBuffer, MEM_LEN_CONFIG);
the config (String cfg) is copied to this area.
char *command = strtok_r(readBuffer, ".", &p);
command gets a pointer to the first adress, the first "." is replaced by NULL
If the device is a button
params[0] = strtok_r(NULL, ".", &p); // pin
params[0] gets the adress after the first "." and the next "." is also replaced by a NULL
params[1] = strtok_r(NULL, ":", &p); // name
params[1] gets the adress after the second "." and replaces the coming ":" by a NULL
AddButton(atoi(params[0]), params[1]);
the function AddButton() gets the number of the button and a pointer which points to the memory area from readBuffer[].
In AddButton() this pointer to the name is stored in
buttons[buttonsRegistered] = MFButton(pin, name);
Isn't this still pointing to the memory area of readBuffer[]?
Or where is my msitake?

@MobiFlight-Admin
Copy link
Collaborator

You probably are completely correct. Maybe this was also not such a big deal in the past because I remember that I used to use String as parameter and later changed it for const char * wherever possible to save memory too... but it could lead to garbled names which in fact i have seen.

@MobiFlight-Admin
Copy link
Collaborator

MobiFlight-Admin commented Sep 17, 2021

Is there a need at all to copy the config buffer into another buffer? Hmm... thinking out loud here... while I am checking the code...

Yes, there is: from the strtok_r documentation
Each delimiter in the original string is replaced by a null character, and the pointer to which lasts points is updated.

So we have to duplicate the string... ok. This is also true for the strtok version (non re-entrant)

@elral
Copy link
Collaborator Author

elral commented Sep 17, 2021

@danecreekphotography, a copy of the config Buffet is not really required, I have a branch where I already tested it. But there is still some work required (after writing a config the Connector gives an error) I know what to do but is a little bit difficult during my vacation ;)
One disadvantage is flickering of the LCD if the Connector connect....
@MobiFlight-Admin, I think it is how Memory is used during run time. I am not the best in this, but my understanding is that this Memory area is only used a second time if now more Memory is available. So it is difficult to forecast...

@github-actions
Copy link

Firmware for this pull request:
Firmware.zip

@github-actions
Copy link

github-actions bot commented Oct 4, 2021

Firmware for this pull request:
Firmware.zip

@neilenns neilenns added this to Review in progress in Initial migration Oct 8, 2021
@neilenns neilenns added the Code cleanup Changes that clean up unused libraries, memory issues, etc. but do not change functionality. label Oct 14, 2021
@neilenns neilenns added this to In progress in Code cleanup Oct 17, 2021
@neilenns neilenns removed this from Review in progress in Initial migration Oct 17, 2021
@neilenns neilenns moved this from In progress to Review in progress in Code cleanup Oct 17, 2021
@MobiFlight-Admin
Copy link
Collaborator

superseeded by #45 or #46

Code cleanup automation moved this from Review in progress to Done Oct 27, 2021
@elral elral deleted the Config branch October 28, 2021 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code cleanup Changes that clean up unused libraries, memory issues, etc. but do not change functionality.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Config memory location can get overwritten in certain conditions
3 participants