Browse Source

Sanitise strncpy to always zero terminate destination.

mingodad 10 years ago
parent
commit
7c1567d39b
3 changed files with 9 additions and 6 deletions
  1. 5 2
      librs232/rs232.h
  2. 2 2
      librs232/rs232_posix.c
  3. 2 2
      librs232/rs232_windows.c

+ 5 - 2
librs232/rs232.h

@@ -9,10 +9,10 @@
  * copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following
  * conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -48,6 +48,9 @@
  #include "librs232/rs232_posix.h"
 #endif
 
+/*Ensure destination string is zero terminated*/
+#define strncpyz(dest, src, size) strncpy(dest, src, size); dest[size-1]='\0'
+
 #ifdef RS232_DEBUG
 const char* rs232_hex_dump(const void *data, unsigned int len);
 const char* rs232_ascii_dump(const void *data, unsigned int len);

+ 2 - 2
librs232/rs232_posix.c

@@ -55,7 +55,7 @@ rs232_init(void)
 
 	memset(p->pt, 0, sizeof(struct rs232_posix_t));
 	memset(p->dev, 0, RS232_STRLEN_DEVICE+1);
-	strncpy(p->dev, RS232_PORT_POSIX, RS232_STRLEN_DEVICE);
+	strncpyz(p->dev, RS232_PORT_POSIX, RS232_STRLEN_DEVICE);
 
 	p->baud = RS232_BAUD_115200;
 	p->data = RS232_DATA_8;
@@ -481,7 +481,7 @@ void
 rs232_set_device(struct rs232_port_t *p, const char *device)
 {
 	DBG("p=%p old=%s new=%s\n", (void *)p, p->dev, device);
-	strncpy(p->dev, device, RS232_STRLEN_DEVICE);
+	strncpyz(p->dev, device, RS232_STRLEN_DEVICE);
 	return;
 }
 

+ 2 - 2
librs232/rs232_windows.c

@@ -91,7 +91,7 @@ rs232_init(void)
 	DBG("p=%p p->pt=%p\n", (void *)p, p->pt);
 
 	memset(p->dev, 0, RS232_STRLEN_DEVICE+1);
-	strncpy(p->dev, RS232_PORT_WIN32, RS232_STRLEN_DEVICE);
+	strncpyz(p->dev, RS232_PORT_WIN32, RS232_STRLEN_DEVICE);
 
 	p->baud = RS232_BAUD_115200;
 	p->data = RS232_DATA_8;
@@ -430,7 +430,7 @@ RS232_LIB void
 rs232_set_device(struct rs232_port_t *p, const char *device)
 {
 	DBG("p=%p old=%s new=%s\n", (void *)p, p->dev, device);
-	strncpy(p->dev, device, RS232_STRLEN_DEVICE);
+	strncpyz(p->dev, device, RS232_STRLEN_DEVICE);
 
 	return;
 }