diff mbox series

[v2.1] romfs/tools: Silence more compiler warnings with GCC 8.1

Message ID 1532351984-13501-1-git-send-email-thuth@redhat.com
State Accepted
Headers show
Series [v2.1] romfs/tools: Silence more compiler warnings with GCC 8.1 | expand

Commit Message

Thomas Huth July 23, 2018, 1:19 p.m. UTC
GCC 8 complains about the following usages of strncpy, too:

create_crc.c:86:3: warning: ‘strncpy’ specified bound 16 equals destination
 size [-Wstringop-truncation]
   strncpy(uHeader.stHeader.version, pcVersion, 16);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
create_crc.c:84:3: warning: ‘strncpy’ specified bound 16 equals destination
 size [-Wstringop-truncation]
   strncpy(uHeader.stHeader.version, pcVersion, 16);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Let's work around the issue by using memcpy instead.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 romfs/tools/create_crc.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

Comments

Alexey Kardashevskiy July 24, 2018, 7:02 a.m. UTC | #1
On 23/07/2018 23:19, Thomas Huth wrote:
> GCC 8 complains about the following usages of strncpy, too:
> 
> create_crc.c:86:3: warning: ‘strncpy’ specified bound 16 equals destination
>  size [-Wstringop-truncation]
>    strncpy(uHeader.stHeader.version, pcVersion, 16);
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> create_crc.c:84:3: warning: ‘strncpy’ specified bound 16 equals destination
>  size [-Wstringop-truncation]
>    strncpy(uHeader.stHeader.version, pcVersion, 16);
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> Let's work around the issue by using memcpy instead.


Thanks, applied this one and the others from v2.
diff mbox series

Patch

diff --git a/romfs/tools/create_crc.c b/romfs/tools/create_crc.c
index 475b184..abc373b 100644
--- a/romfs/tools/create_crc.c
+++ b/romfs/tools/create_crc.c
@@ -32,6 +32,11 @@  static uint64_t ui64globalHeaderSize = 0;
 /* flag to filter detect the header in buildDataStream() */
 static int iglobalHeaderFlag = 1;
 
+static size_t min(size_t a, size_t b)
+{
+	return a < b ? a : b;
+}
+
 /**
  * Build the file image and store it as Data Stream of bytes
  * calculate a first CRC for the first file and
@@ -80,13 +85,13 @@  createHeaderImage(int notime)
 	};
 
 	/* read driver info */
-	if (NULL != (pcVersion = getenv("DRIVER_NAME"))) {
-		strncpy(stHeader.version, pcVersion, 16);
-	} else if (NULL != (pcVersion = getenv("USER"))) {
-		strncpy(stHeader.version, pcVersion, 16);
-	} else if (pcVersion == NULL) {
-		strncpy(stHeader.version, "No known user!", 16);
-	}
+	pcVersion = getenv("DRIVER_NAME");
+	if (!pcVersion)
+		pcVersion = getenv("USER");
+	if (!pcVersion)
+		pcVersion = "unknown";
+	memcpy(stHeader.version, pcVersion,
+	       min(strlen(pcVersion), sizeof(stHeader.version)));
 
 	if (!notime) {
 		/* read time and write it into data stream */