diff options
author | Xavier Roche <xroche@users.noreply.github.com> | 2014-07-28 20:56:38 +0000 |
---|---|---|
committer | Xavier Roche <xroche@users.noreply.github.com> | 2014-07-28 20:56:38 +0000 |
commit | 0ac9c0df327e70e02c471d94f39e3643eb302a4e (patch) | |
tree | d592a76822b0cd881922c812e759b519f2d71042 | |
parent | da41e9a7a174334f3d9c232ad9b45e952018cee6 (diff) |
Fixed 'strlen(copyBuff) == qLen failed at htslib.c:3458' assertion failure.
Rationale: strncat(..., ..., (size_t) -1) does not behave gently on Linux, and is not equivalent to strcat(..., ...) when using optimizations (could it be a corner-case bug ?)
-rw-r--r-- | src/htssafe.h | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/htssafe.h b/src/htssafe.h index d21107c..3451d26 100644 --- a/src/htssafe.h +++ b/src/htssafe.h @@ -130,12 +130,24 @@ static HTS_UNUSED void htssafe_compile_time_check_(void) { */ #define strncatbuff(A, B, N) \ ( HTS_IS_NOT_CHAR_BUFFER(A) \ - ? strncat(A, B, N) \ + ? ( (N) != (size_t) -1 ? strncat(A, B, N) : strcat(A, B) ) \ : strncat_safe_(A, sizeof(A), B, \ HTS_IS_NOT_CHAR_BUFFER(B) ? (size_t) -1 : sizeof(B), N, \ "overflow while appending '" #B "' to '"#A"'", __FILE__, __LINE__) ) /** + * 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 strcatbuff(A, B) \ + ( HTS_IS_NOT_CHAR_BUFFER(A) \ + ? strcat(A, B) \ + : strncat_safe_(A, sizeof(A), B, \ + HTS_IS_NOT_CHAR_BUFFER(B) ? (size_t) -1 : sizeof(B), (size_t) -1, \ + "overflow while appending '" #B "' to '"#A"'", __FILE__, __LINE__) ) + +/** * 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. @@ -147,15 +159,6 @@ static HTS_UNUSED void htssafe_compile_time_check_(void) { 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" */ - -/** - * 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 strcatbuff(A, B) strncatbuff(A, B, (size_t) -1) - /** * Append characters of "B" to "A", "A" having a maximum capacity of "S". */ @@ -188,6 +191,7 @@ static HTS_INLINE HTS_UNUSED char* strncat_safe_(char *const dest, const size_t const char *exp, const char *file, int line) { const size_t source_len = strlen_safe_(source, sizeof_source, file, line); const size_t dest_len = strlen_safe_(dest, sizeof_dest, file, line); + /* note: "size_t is an unsigned integral type" ((size_t) -1 is positive) */ const size_t source_copy = source_len <= n ? source_len : n; const size_t dest_final_len = dest_len + source_copy; assertf__(dest_final_len < sizeof_dest, exp, file, line); |