cedric pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=02bcdf056eb59758115bde8b6073f0b95a4efd40

commit 02bcdf056eb59758115bde8b6073f0b95a4efd40
Author: Cedric BAIL <[email protected]>
Date:   Fri Oct 31 02:40:08 2014 +0100

    eina: clear up eina_tmpstr length information
    
    This @fix a potential wrong return value according to documentation and
    improve consistency accross this API. This should fix T1775.
---
 src/lib/eina/eina_tmpstr.c | 8 +++++---
 src/lib/eina/eina_tmpstr.h | 7 ++++++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/lib/eina/eina_tmpstr.c b/src/lib/eina/eina_tmpstr.c
index 711a175..57f3c2b 100644
--- a/src/lib/eina/eina_tmpstr.c
+++ b/src/lib/eina/eina_tmpstr.c
@@ -72,12 +72,14 @@ eina_tmpstr_add_length(const char *str, size_t length)
    Str *s;
 
    if (!str || !length) return NULL;
-   s = malloc(sizeof(Str) + length + 1);
+   /* eina_tmpstr_strlen is expected to return strlen + 1 */
+   length += 1;
+   s = malloc(sizeof(Str) + length);
    if (!s) return NULL;
    s->length = length;
    s->str = ((char *)s) + sizeof(Str);
-   strncpy(s->str, str, length);
-   s->str[length] = '\0';
+   strncpy(s->str, str, length - 1);
+   s->str[length - 1] = '\0';
    eina_lock_take(&_mutex);
    s->next = strs;
    strs = s;
diff --git a/src/lib/eina/eina_tmpstr.h b/src/lib/eina/eina_tmpstr.h
index cd1589a..7460356 100644
--- a/src/lib/eina/eina_tmpstr.h
+++ b/src/lib/eina/eina_tmpstr.h
@@ -184,7 +184,12 @@ EAPI Eina_Tmpstr *eina_tmpstr_add(const char *str) 
EINA_WARN_UNUSED_RESULT;
  * printf("%s\n", tmpstr) etc.). This string should be considered read-only
  * and immutable, and when youa re done with the string yo should delete it
  * with eina_tmpstr_del().
- * 
+ *
+ * @note If the length is greater than the actual string, but still '\0'
+ *       terminateed. Their won't be any crash and the string will be correct,
+ *       but eina_tmpstr_strlen will return an erroneous length. So if you
+ *       want to have the correct length always call eina_tmpstr_add_length
+ *       with length == strlen(str).
  * @see eina_tmpstr_del()
  * @see eina_tmpstr_add()
  * 

-- 


Reply via email to