summaryrefslogtreecommitdiff
path: root/src/htssafe.h
diff options
context:
space:
mode:
authorXavier Roche <xroche@users.noreply.github.com>2014-05-06 18:40:50 +0000
committerXavier Roche <xroche@users.noreply.github.com>2014-05-06 18:40:50 +0000
commit65eb4e58e58672ccd7f9dcbc0205b49055bbdb7b (patch)
treec6724c6927cb16401f3e9234a202a5ce5d947ba6 /src/htssafe.h
parent7e753143950624c7457b322b6094c1e4dddd0554 (diff)
Fixed regression introduced in r995 over strcpybuff() reusing macro arguments multiple times, leading to troubles when using side-effects.
Diffstat (limited to 'src/htssafe.h')
-rw-r--r--src/htssafe.h32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/htssafe.h b/src/htssafe.h
index 1181bde..004b068 100644
--- a/src/htssafe.h
+++ b/src/htssafe.h
@@ -101,23 +101,28 @@ static HTS_UNUSED void abortf_(const char *exp, const char *file, int line) {
? strncat(A, B, N) \
: strncat_safe_(A, sizeof(A), B, \
HTS_IS_NOT_CHAR_BUFFER(B) ? (size_t) -1 : sizeof(B), N, \
- "overflow while copying '" #B "' to '"#A"'", __FILE__, __LINE__) )
-
-/* note: "size_t is an unsigned integral type" */
+ "overflow while appending '" #B "' to '"#A"'", __FILE__, __LINE__) )
/**
- * Append characters of "B" to "A".
+ * Copy characters from "B" to "A".
* If "A" is a char[] variable whose size is not sizeof(char*), then the size
* is assumed to be the capacity of this array.
*/
-#define strcatbuff(A, B) strncatbuff(A, B, (size_t) -1)
+#define strcpybuff(A, B) \
+ ( HTS_IS_NOT_CHAR_BUFFER(A) \
+ ? strcpy(A, B) \
+ : strcpy_safe_(A, sizeof(A), B, \
+ HTS_IS_NOT_CHAR_BUFFER(B) ? (size_t) -1 : sizeof(B), \
+ "overflow while copying '" #B "' to '"#A"'", __FILE__, __LINE__) )
+
+/* note: "size_t is an unsigned integral type" */
/**
- * Copy characters of "B" to "A".
+ * Append characters of "B" to "A".
* If "A" is a char[] variable whose size is not sizeof(char*), then the size
* is assumed to be the capacity of this array.
*/
-#define strcpybuff(A, B) (clear_buffer_(A), strcatbuff(A, B))
+#define strcatbuff(A, B) strncatbuff(A, B, (size_t) -1)
/**
* Append characters of "B" to "A", "A" having a maximum capacity of "S".
@@ -136,11 +141,6 @@ static HTS_INLINE HTS_UNUSED size_t strlen_safe_(const char *source, const size_
return size;
}
-static HTS_INLINE HTS_UNUSED char* clear_buffer_(char *buffer) {
- buffer[0] = '\0';
- return buffer;
-}
-
static HTS_INLINE HTS_UNUSED char* strncat_safe_(char *const dest, const size_t sizeof_dest,
const char *const source, const size_t sizeof_source,
const size_t n,
@@ -155,6 +155,14 @@ static HTS_INLINE HTS_UNUSED char* strncat_safe_(char *const dest, const size_t
return dest;
}
+static HTS_INLINE HTS_UNUSED char* strcpy_safe_(char *const dest, const size_t sizeof_dest,
+ const char *const source, const size_t sizeof_source,
+ const char *exp, const char *file, int line) {
+ assertf_(sizeof_dest != 0, file, line);
+ dest[0] = '\0';
+ return strncat_safe_(dest, sizeof_dest, source, sizeof_source, (size_t) -1, exp, file, line);
+}
+
#define malloct(A) malloc(A)
#define calloct(A,B) calloc((A), (B))
#define freet(A) do { if ((A) != NULL) { free(A); (A) = NULL; } } while(0)