git.samba.org
/
sfrench
/
samba-autobuild
/
.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
46d369b
)
r25209: unify logic of talloc_vasprintf_append() and talloc_vasprintf_append_buffer()
author
Stefan Metzmacher
<metze@samba.org>
Tue, 18 Sep 2007 11:56:24 +0000
(11:56 +0000)
committer
Gerald (Jerry) Carter
<jerry@samba.org>
Wed, 10 Oct 2007 20:06:53 +0000
(15:06 -0500)
metze
(This used to be commit
780c1c776e15f7224256a0ba0bda3e9f04702abe
)
source4/lib/talloc/talloc.c
patch
|
blob
|
history
diff --git
a/source4/lib/talloc/talloc.c
b/source4/lib/talloc/talloc.c
index 0b4d977aa7e9c76edddea62febc4c94aae413abb..033e70d2f963069b95cd284cc540d534b1172df7 100644
(file)
--- a/
source4/lib/talloc/talloc.c
+++ b/
source4/lib/talloc/talloc.c
@@
-1313,30
+1313,18
@@
char *talloc_asprintf(const void *t, const char *fmt, ...)
return ret;
}
return ret;
}
-
-/**
- * Realloc @p s to append the formatted result of @p fmt and @p ap,
- * and return @p s, which may have moved. Good for gradually
- * accumulating output into a string buffer. Appends at the end
- * of the string.
- **/
-char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
+static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
+ const char *fmt, va_list ap)
{
{
-
int len, s_
len;
+
ssize_t a
len;
va_list ap2;
char c;
va_list ap2;
char c;
- if (s == NULL) {
- return talloc_vasprintf(NULL, fmt, ap);
- }
-
- s_len = strlen(s);
-
va_copy(ap2, ap);
va_copy(ap2, ap);
- len = vsnprintf(&c, 1, fmt, ap2);
+
a
len = vsnprintf(&c, 1, fmt, ap2);
va_end(ap2);
va_end(ap2);
- if (len <= 0) {
+ if (
a
len <= 0) {
/* Either the vsnprintf failed or the format resulted in
* no characters being formatted. In the former case, we
* ought to return NULL, in the latter we ought to return
/* Either the vsnprintf failed or the format resulted in
* no characters being formatted. In the former case, we
* ought to return NULL, in the latter we ought to return
@@
-1346,17
+1334,32
@@
char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
return s;
}
return s;
}
- s = talloc_realloc(NULL, s, char, s
_len + len+
1);
+ s = talloc_realloc(NULL, s, char, s
len + alen +
1);
if (!s) return NULL;
va_copy(ap2, ap);
if (!s) return NULL;
va_copy(ap2, ap);
- vsnprintf(s
+s_len, len+
1, fmt, ap2);
+ vsnprintf(s
+ slen, alen +
1, fmt, ap2);
va_end(ap2);
va_end(ap2);
- _talloc_set_name_const(s, s);
+ _talloc_set_name_const(s, s);
return s;
}
return s;
}
+/**
+ * Realloc @p s to append the formatted result of @p fmt and @p ap,
+ * and return @p s, which may have moved. Good for gradually
+ * accumulating output into a string buffer. Appends at the end
+ * of the string.
+ **/
+char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
+{
+ if (unlikely(!s)) {
+ return talloc_vasprintf(NULL, fmt, ap);
+ }
+
+ return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
+}
+
/**
* Realloc @p s to append the formatted result of @p fmt and @p ap,
* and return @p s, which may have moved. Always appends at the
/**
* Realloc @p s to append the formatted result of @p fmt and @p ap,
* and return @p s, which may have moved. Always appends at the
@@
-1364,42
+1367,18
@@
char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
**/
char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
{
**/
char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
{
- struct talloc_chunk *tc;
- int len, s_len;
- va_list ap2;
- char c;
+ size_t slen;
- if (
s == NULL
) {
+ if (
unlikely(!s)
) {
return talloc_vasprintf(NULL, fmt, ap);
}
return talloc_vasprintf(NULL, fmt, ap);
}
- tc = talloc_chunk_from_ptr(s);
-
- s_len = tc->size - 1;
-
- va_copy(ap2, ap);
- len = vsnprintf(&c, 1, fmt, ap2);
- va_end(ap2);
-
- if (len <= 0) {
- /* Either the vsnprintf failed or the format resulted in
- * no characters being formatted. In the former case, we
- * ought to return NULL, in the latter we ought to return
- * the original string. Most current callers of this
- * function expect it to never return NULL.
- */
- return s;
+ slen = talloc_get_size(s);
+ if (likely(slen > 0)) {
+ slen--;
}
}
- s = talloc_realloc(NULL, s, char, s_len + len+1);
- if (!s) return NULL;
-
- va_copy(ap2, ap);
- vsnprintf(s+s_len, len+1, fmt, ap2);
- va_end(ap2);
- _talloc_set_name_const(s, s);
-
- return s;
+ return __talloc_vaslenprintf_append(s, slen, fmt, ap);
}
/*
}
/*