nqc icon indicating copy to clipboard operation
nqc copied to clipboard

Switch usages of strlcpy and strlcat to more cross-compatible methods

Open JamesHagerman opened this issue 4 years ago • 3 comments

When compiling this under Windows Subsystem for Linux (WSL) using Ubuntu, I was able to get this to compile using the following steps:

  1. Install the build tool chain and the dependencies:

sudo apt install build-essential bison flex

  1. Patch nqc/DirList.cpp to replace strlcpy() and strlcat() with strncpy() and strcat() respectively. Usage of sizeof() worked to at least compile bin/nqc. Here's the git diff showing the patched lines. (Note: I'm not sure this is the right way, but it should be close... I think???)
diff --git a/nqc/DirList.cpp b/nqc/DirList.cpp
index 766b2b4..6bccc93 100644
--- a/nqc/DirList.cpp
+++ b/nqc/DirList.cpp
@@ -53,7 +53,7 @@ bool DirList::Find(const char *filename, char *pathname)
     struct stat stat_buf;

     size_t len = sizeof(pathname);
-    if (strlcpy(pathname, filename, len) >= len) {
+    if (sizeof(strncpy(pathname, filename, len)) >= len) {
         return false;
     }

@@ -61,8 +61,8 @@ bool DirList::Find(const char *filename, char *pathname)
         return true;

     for(Entry *e = fEntries.GetHead(); e; e=e->GetNext()) {
-        if (strlcpy(pathname, e->GetPath(), len) < len) {
-            if (strlcat(pathname, filename, len) < len) {
+        if (sizeof(strncpy(pathname, e->GetPath(), len)) < len) {
+            if (sizeof(strncat(pathname, filename, len)) < len) {
                 if (stat(pathname, &stat_buf) == 0) {
                     return true;
                 }
  1. Run make from the root of the repository
  2. Run bin/nqc

If this sounds acceptable, I'll open a PR for the change.

JamesHagerman avatar Sep 04 '20 07:09 JamesHagerman

It probably used strncpy and strcat historically before I introduced the "safer" versions, so you should be able to check history. I never really completed the conversion to this API, but ideally this would be handled as a cross-platform compile config rather than reverting the use completely. Esepcially since those two routines are in the project already.

If I get some time I'll take a look at finishing the conversion and either abandoning it or adding the glue to make it work on GNUish platforms.

jverne avatar Oct 03 '22 15:10 jverne

You can upstream those changes: https://github.com/BrickBot/nqc/blob/master/nqc/DirList.cpp

They seem to work fine on Ubuntu 22.04.

davidperrenoud avatar Oct 11 '22 12:10 davidperrenoud

Hello from the BrickBot org! Linux builds are supported in the codebase there (including a GitHub action set to run on Linux), and NQC-related packages are available for the Slitaz Linux OS. Glad to hear of the good report regarding Ubuntu!

mesheets avatar Feb 11 '23 17:02 mesheets