rkdeveloptool icon indicating copy to clipboard operation
rkdeveloptool copied to clipboard

Build error in latest master

Open buelowp opened this issue 3 years ago • 9 comments

Later versions of GCC have started to generate the warning

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);
      |                                           ^~

and because -Wall and -Werror are used, this causes the build to fail. I've attached the patch I used, but there may be a better solution.

diff --git a/main.cpp b/main.cpp
index 72bd94b..ec5257b 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1489,8 +1489,12 @@ static bool saveEntry(FILE* outFile, char* path, rk_entry_type type,
 
 static inline uint32_t convertChipType(const char* chip) {
        char buffer[5];
+        int ret = 0;
+
        memset(buffer, 0, sizeof(buffer));
-       snprintf(buffer, sizeof(buffer), "%s", chip);
+       if ((ret = snprintf(buffer, sizeof(buffer), "%s", chip))) {
+            perror("snprintf");
+        }
        return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
 }

buelowp avatar Aug 19 '22 15:08 buelowp

I just ran into the same problem and found this ticket by googling. Thanks for creating it.

@buelowp, I think your patch is close, but it will print a lot of error messages. snprintf returns the number of bytes written, so the error message will happen whenever a nonzero number of bytes were written. (Which is probably usually true.)

Infinoid avatar Aug 21 '22 22:08 Infinoid

I modified my copy slightly to check if ret > 4.

Infinoid avatar Aug 21 '22 22:08 Infinoid

Cheers! Thanks for the patch

ChrisJMacdonald avatar Sep 13 '22 23:09 ChrisJMacdonald

Thnx for the fix!

DariusKaz avatar Nov 05 '22 19:11 DariusKaz

What do you think about this alternative solution: https://github.com/martinSusz/rkdeveloptool/commit/1e6322cafb6e7d5b08c0427e890fa6bc89a535e2 ?

martinSusz avatar Dec 03 '22 00:12 martinSusz

I'm a noob in the blind trying to solve this issue. How do I actually run this patch?

I've tried entering the rkdeveloptool folder and pasting the whole thing in the terminal but get a "zsh: parse error near `}'". Am I on the right track at all?

brrrglund avatar Jun 15 '23 16:06 brrrglund

Would also love to know how to apply this

Pinjontall94 avatar Feb 06 '24 03:02 Pinjontall94

So the above solution is not a command to run, but is showing the Git Diff between the original main.cpp file, and the patched version (More on reading these diffs here: https://www.atlassian.com/git/tutorials/saving-changes/git-diff)

Essentially, you want to open the main.cpp file, find the lines referenced above, remove the lines that have -- prefix, and add in the new lines that have the ++prefix. Then save and try run šŸ‘

ChrisJMacdonald avatar Feb 06 '24 06:02 ChrisJMacdonald

diff --git a/main.cpp b/main.cpp
index 72bd94b..ec5257b 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1489,8 +1489,12 @@ static bool saveEntry(FILE* outFile, char* path, rk_entry_type type,
 
 static inline uint32_t convertChipType(const char* chip) {
        char buffer[5];
+        int ret = 0;
+
        memset(buffer, 0, sizeof(buffer));
-       snprintf(buffer, sizeof(buffer), "%s", chip);
+       if ((ret = snprintf(buffer, sizeof(buffer), "%s", chip)) > 4) {
+            perror("snprintf");
+        }
        return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
 }

Copy this into a file called snprintf.patch and save it to the same folder as main.cpp. Then, run patch -p0 < snprintf.patch. I can't really get into the details of how to use patch or patch for Windows here, there are a LOT of google results to help with that. However, that short workflow will apply the patch.

Then you must build it, and once built, you can use it.

buelowp avatar Feb 06 '24 15:02 buelowp