rockchip-linux / rkdeveloptool

GNU General Public License v2.0
146 stars 85 forks source link

Build error in latest master #70

Open buelowp opened 1 year ago

buelowp commented 1 year ago

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];
 }
Infinoid commented 1 year ago

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 commented 1 year ago

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

ChrisJMacdonald commented 1 year ago

Cheers! Thanks for the patch

DariusKaz commented 1 year ago

Thnx for the fix!

martinSusz commented 1 year ago

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

brrrglund commented 1 year ago

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?

Pinjontall94 commented 5 months ago

Would also love to know how to apply this

ChrisJMacdonald commented 5 months ago

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 👍

buelowp commented 5 months ago
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.