From 94c8faab9f5b7dfcf11b6a0084cf54029badb125 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周日, 24 4月 2016 18:25:36 +0800
Subject: [PATCH] Fix tests for device dependent info, convert to strndup, fix out of bounds access

---
 libscpi/src/utils.c |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index 99dfd06..ae2c2c6 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -760,7 +760,7 @@
  * @param s - current pointer of duplication string
  * @return - pointer of duplicated string or NULL, if duplicate is not possible.
  */
-char * OUR_strdup(scpi_error_info_heap_t * heap, const char *s) {
+char * OUR_strndup(scpi_error_info_heap_t * heap, const char *s, size_t n) {
     if (!s || !heap) {
         return NULL;
     }
@@ -773,14 +773,13 @@
         return NULL;
     }
 
-    size_t len = strlen(s) + 1; // additional '\0' at end
+    size_t len = SCPIDEFINE_strnlen(s, n) + 1; // additional '\0' at end
     if (len > heap->count) {
         return NULL;
     }
-    char * ptrs = s;
+    const char * ptrs = s;
     char * head = &heap->data[heap->wr];
     size_t rem = heap->size - (&heap->data[heap->wr] - heap->data);
-    size_t sstp = 0;
 
     if (len >= rem) {
         memcpy(&heap->data[heap->wr], s, rem);
@@ -836,14 +835,14 @@
  * @param s - pointer of duplicate string
  * @param rollback - backward write pointer in heap
  */
-void OUR_free(scpi_error_info_heap_t * heap, const char * s, scpi_bool_t rollback) {
+void OUR_free(scpi_error_info_heap_t * heap, char * s, scpi_bool_t rollback) {
 
     if (!s) return;
 
     char * data_add;
     size_t len[2];
 
-    if (!OUR_get_parts(heap, s, &len[0], &data_add, &len[1])) return;
+    if (!OUR_get_parts(heap, s, &len[0], (const char **)&data_add, &len[1])) return;
 
     if (data_add) {
         len[1]++;

--
Gitblit v1.9.1