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

Small fixes for LCD text printing #155

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

nica-f
Copy link

@nica-f nica-f commented Jan 20, 2019

This PR includes two smaller fixes around printing of text.
First fix is resolving buffer overflows when compiling from GIT and Version_GetGitVersion() returns a pretty long string, which overflows the LCD_disp_str buffer (and causes a crash and reset).
The second fix makes some more room in the setup screen so that higher offsets with one digit more can be displayed - previously it crashed for me too for offsets not fitting the screen anymore.

@@ -118,7 +118,7 @@ int main(void) {
LCD_disp_str((uint8_t*)buf, len, 0, 64 - 6, FONT6X6);
printf("\nRunning on an %s", buf);

len = snprintf(buf, sizeof(buf), "%s", Version_GetGitVersion());
Copy link

Choose a reason for hiding this comment

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

This introduces a potential buyer overflow for future devs, try sizeof(buf) - 1

Copy link
Author

Choose a reason for hiding this comment

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

Oh wow, that was fast

As far as I have understood the original problem, buf is not the
problem. The problem is that the LCD_disp_str() function also uses fixed
length buffers according to display pixel size and these buffers will
overflow if you try to print texts resulting in more pixels than the
display width. At a 6x6 font the maximum is 21 characters, that's why I
limited to it.

Does that explain it a bit?

Copy link

Choose a reason for hiding this comment

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

In that case, I would suggest introducing a common define used between the output code and buffer allocation, which makes the relationship explicit

@mikeanton
Copy link

You don't need to worry about using sizeof(buf)-1, as the snprintf function handles that for you. Just pass the size of the buffer, and it reserves enough space for the terminating null character according to this reference: http://www.cplusplus.com/reference/cstdio/snprintf/.

@deece
Copy link

deece commented Jan 21, 2019

@mikeanton that's C++, the C implementation does not guarantee a null terminated string on truncation. https://linux.die.net/man/3/snprintf

@mikeanton
Copy link

@deece The reference you linked to says exactly the same thing: that the number of bytes written includes the terminating null character. Therefore, using sizeof(buf)-1 is not required.

@deece
Copy link

deece commented Jan 21, 2019

You're right, i just confirmed that a truncated string format will insert a null byte.

It seems I've been tainted by _snprintf from Microsoft :)

@pReya
Copy link

pReya commented Feb 13, 2019

Seems like @xnk is not responding anymore :( Would love to get this merged.

@xnk
Copy link
Member

xnk commented Feb 13, 2019 via email

@pReya
Copy link

pReya commented Feb 15, 2019

Thanks @xnk! Appreciate your work!

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.

5 participants