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.
This commit is contained in:
Cedric BAIL 2014-10-31 02:40:08 +01:00
parent f4ba6fdc2d
commit 02bcdf056e
2 changed files with 11 additions and 4 deletions

View File

@ -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;

View File

@ -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()
*