From 22ad4f7200a56da969e9296d8fecf2f0b8368afc Mon Sep 17 00:00:00 2001
From: Dan Yeaw <dyeaw@ford.com>
Date: Mon, 23 Dec 2019 15:31:04 -0500
Subject: [PATCH] Bootloader: Fix GCC warnings for strncpy and strncat.

Fixes Issue #4196. GCC 8.1 and later added checks to prevent using
strncpy and strncat with the bounds depending on the length of the
source str. In order to fix this, a few approaches were made:
1. Where guards already existed to ensure that added paths weren't
larger than the PATH_MAX, changed to strcpy / strcat which don't take a
length parameter.
2. Where path length checks didn't exist, set the bounds of the strncpy
and strncat to PATH_MAX.

Signed-off-by: Dan Yeaw <dyeaw@ford.com>
---
 bootloader/src/pyi_path.c      |  7 ++++---
 bootloader/src/pyi_pythonlib.c | 12 ++++++------
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/bootloader/src/pyi_path.c b/bootloader/src/pyi_path.c
index a449f79..d36e3d2 100644
--- a/bootloader/src/pyi_path.c
+++ b/bootloader/src/pyi_path.c
@@ -56,7 +56,7 @@ pyi_path_dirname(char *result, const char *path)
     char *match = NULL;
 
     /* Copy path to result and then just write '\0' to the place with path separator. */
-    strncpy(result, path, strlen(path) + 1);
+    strncpy(result, path, PATH_MAX);
     /* Remove separator from the end. */
     len = strlen(result);
 
@@ -143,7 +143,7 @@ pyi_path_join(char *result, const char *path1, const char *path2)
         memset(result, 0, PATH_MAX);
     }
     /* Copy path1 to result without null terminator */
-    strncpy(result, path1, strlen(path1));
+    strcpy(result, path1);
     /* Append trailing slash if missing. */
     len = strlen(result);
 
@@ -156,7 +156,8 @@ pyi_path_join(char *result, const char *path1, const char *path2)
 
     if (path2[len - 1] == PYI_SEP) {
         /* Append path2 without slash. */
-        strncat(result, path2, len - 2);
+        strcat(result, path2);
+        result[strlen(result) - 1] = PYI_NULLCHAR;
     }
     else {
         /* path2 does not end with slash. */
diff --git a/bootloader/src/pyi_pythonlib.c b/bootloader/src/pyi_pythonlib.c
index ee4b3b2..d90c239 100644
--- a/bootloader/src/pyi_pythonlib.c
+++ b/bootloader/src/pyi_pythonlib.c
@@ -480,15 +480,15 @@ pyi_pylib_start_python(ARCHIVE_STATUS *status)
     /* Set sys.path */
     if (is_py2) {
         /* sys.path = [mainpath] */
-        strncpy(pypath, status->mainpath, strlen(status->mainpath));
+        strncpy(pypath, status->mainpath, PATH_MAX);
     }
     else {
         /* sys.path = [base_library, mainpath] */
-        strncpy(pypath, status->mainpath, strlen(status->mainpath));
-        strncat(pypath, PYI_SEPSTR, strlen(PYI_SEPSTR));
-        strncat(pypath, "base_library.zip", strlen("base_library.zip"));
-        strncat(pypath, PYI_PATHSEPSTR, strlen(PYI_PATHSEPSTR));
-        strncat(pypath, status->mainpath, strlen(status->mainpath));
+        strncpy(pypath, status->mainpath, PATH_MAX);
+        strncat(pypath, PYI_SEPSTR, PATH_MAX);
+        strncat(pypath, "base_library.zip", PATH_MAX);
+        strncat(pypath, PYI_PATHSEPSTR, PATH_MAX);
+        strncat(pypath, status->mainpath, PATH_MAX);
     };
 
     /*
-- 
2.23.0

