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

Update main.cpp #97

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

Conversation

tenxer-roy
Copy link

@tenxer-roy tenxer-roy commented Feb 7, 2024

Compiling error:

[email protected]: ~ /rkdeveloptool$ make
make[1]: Entering directory '/home/roy/rkdeveloptool'
g++ -DHAVE_CONFIG_H -I. -I./cfg -Wall -Werror -Wextra -Wreturn-type -fno-strict-aliasing -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -I/usr/include/libusb-1.0 -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
main.cpp: In function ‘bool _Z9mergeBootv.part.0()’:
main.cpp:1493:43: error: ‘%s’ directive output may be truncated writing up to 557 bytes into a region of size 5 [-Werror=format-truncation=]
1493 | snprintf(buffer, sizeof(buffer), "%s", chip);
| ^~
......
1534 | chipType = convertChipType(chip + 2);
| ~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:894,
from DefineHeader.h:3,
from main.cpp:11:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output between 1 and 558 bytes into a destination of size 5
71 | return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72 | __glibc_objsize (__s), __fmt,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73 | __va_arg_pack ());
| ~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors
make[1]: *** [Makefile:491: main.o] Error 1
make[1]: Leaving directory '/home/roy/rkdeveloptool'
make: *** [Makefile:511: all-recursive] Error 1

Possible reason:

The error is due to a format-truncation warning being treated as an error during compilation. This occurs when you're trying to write more characters to a buffer than it can hold, which can lead to buffer overflow issues.

To fix this issue, you need to ensure that the buffer size used in the snprintf call is large enough to accommodate the string being formatted. In this case, it seems that the buffer size is set to 5 bytes, but the string being formatted (chip) might be longer than that.

Fix:

Mentioning the length in line 1493 (main.cpp)

snprintf(buffer, sizeof(buffer), "%.4s", chip);

Compiling error:
=================================================
[email protected]: ~/rkdeveloptool$ make
make[1]: Entering directory '/home/roy/rkdeveloptool'
g++ -DHAVE_CONFIG_H -I. -I./cfg  -Wall -Werror -Wextra -Wreturn-type -fno-strict-aliasing -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE -I/usr/include/libusb-1.0   -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
main.cpp: In function ‘bool _Z9mergeBootv.part.0()’:
main.cpp:1493:43: error: ‘%s’ directive output may be truncated writing up to 557 bytes into a region of size 5 [-Werror=format-truncation=]
 1493 |         snprintf(buffer, sizeof(buffer), "%s", chip);
      |                                           ^~
......
 1534 |                 chipType = convertChipType(chip + 2);
      |                            ~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:894,
                 from DefineHeader.h:3,
                 from main.cpp:11:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output between 1 and 558 bytes into a destination of size 5
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors
make[1]: *** [Makefile:491: main.o] Error 1
make[1]: Leaving directory '/home/roy/rkdeveloptool'
make: *** [Makefile:511: all-recursive] Error 1



Possible reason:
=================================================
The error is due to a format-truncation warning being treated as an error during compilation. This occurs when you're trying to write more characters to a buffer than it can hold, which can lead to buffer overflow issues.

To fix this issue, you need to ensure that the buffer size used in the snprintf call is large enough to accommodate the string being formatted. In this case, it seems that the buffer size is set to 5 bytes, but the string being formatted (chip) might be longer than that.


Fix:
=================================================
Mentioning the length in line 1493 (main.cpp)

snprintf(buffer, sizeof(buffer), "%.4s", chip);
@deliodc
Copy link

deliodc commented Feb 22, 2024

Tested on Debian 12 vagrant machine, fixes the issue.

@robvdl
Copy link

robvdl commented May 20, 2024

This fix is needed for rkdeveloptool to compile on Ubuntu 22.04 LTS, without this fix it won't compile.

Please look into getting this merged.

@inobelar
Copy link

inobelar commented Jul 2, 2024

+1 I have error on gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0. @tenxer-roy thanks for the fix!

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.

4 participants