svn commit: samba r17477 - in branches/SAMBA_3_0/source/lib: .

vlendec at samba.org vlendec at samba.org
Thu Aug 10 11:33:43 GMT 2006


Author: vlendec
Date: 2006-08-10 11:33:42 +0000 (Thu, 10 Aug 2006)
New Revision: 17477

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=17477

Log:
Add talloc_asprintf_len and make use of it.

Volker
Modified:
   branches/SAMBA_3_0/source/lib/talloc.c
   branches/SAMBA_3_0/source/lib/tdb_multikey.c


Changeset:
Modified: branches/SAMBA_3_0/source/lib/talloc.c
===================================================================
--- branches/SAMBA_3_0/source/lib/talloc.c	2006-08-10 10:31:27 UTC (rev 17476)
+++ branches/SAMBA_3_0/source/lib/talloc.c	2006-08-10 11:33:42 UTC (rev 17477)
@@ -1136,7 +1136,47 @@
 	return ret;
 }
 
+int talloc_vasprintf_len(const void *t, char **res, const char *fmt,
+			 va_list ap)
+{	
+	int len;
+	va_list ap2;
+	char c;
+	
+	VA_COPY(ap2, ap);
 
+	/* this call looks strange, but it makes it work on older solaris boxes */
+	if ((len = vsnprintf(&c, 1, fmt, ap2)) < 0) {
+		return len;
+	}
+
+	*res = (char *)_talloc(t, len+1);
+	if (*res) {
+		VA_COPY(ap2, ap);
+		vsnprintf(*res, len+1, fmt, ap2);
+		talloc_set_name_const(*res, *res);
+	}
+
+	return len;
+}
+
+
+/*
+  Perform string formatting, and return a pointer to newly allocated
+  memory holding the result, inside a memory pool.
+ */
+int talloc_asprintf_len(const void *t, char **res, const char *fmt, ...)
+{
+	va_list ap;
+	int len;
+
+	va_start(ap, fmt);
+	len = talloc_vasprintf_len(t, res, fmt, ap);
+	va_end(ap);
+	return len;
+}
+
+
 /**
  * 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

Modified: branches/SAMBA_3_0/source/lib/tdb_multikey.c
===================================================================
--- branches/SAMBA_3_0/source/lib/tdb_multikey.c	2006-08-10 10:31:27 UTC (rev 17476)
+++ branches/SAMBA_3_0/source/lib/tdb_multikey.c	2006-08-10 11:33:42 UTC (rev 17477)
@@ -140,13 +140,14 @@
 
 	prim.dptr = data.dptr = NULL;
 
-	key.dptr = talloc_asprintf(ctx, "KEY/%d/%s", keynumber, value);
+	key.dsize = talloc_asprintf_len(ctx, &key.dptr, "KEY/%d/%s", keynumber,
+					value);
 	if (key.dptr == NULL) {
 		DEBUG(0, ("talloc_asprintf failed\n"));
 		status = NT_STATUS_NO_MEMORY;
 		goto fail;
 	}
-	key.dsize = strlen(key.dptr)+1;
+	key.dsize += 1;
 
 	prim = tdb_fetch(tdb, key);
 	if (prim.dptr == NULL) {
@@ -214,13 +215,14 @@
 		NTSTATUS status;
 		TDB_DATA key;
 
-		key.dptr = talloc_asprintf(keys, "KEY/%d/%s", i, keys[i]);
+		key.dsize = talloc_asprintf_len(keys, &key.dptr, "KEY/%d/%s",
+						i, keys[i]);
 		if (key.dptr == NULL) {
 			DEBUG(0, ("talloc_asprintf failed\n"));
 			TALLOC_FREE(keys);
 			return NT_STATUS_NO_MEMORY;
 		}
-		key.dsize = strlen(key.dptr)+1;
+		key.dsize += 1;
 
 		if (tdb_store(tdb, key, primary_key, TDB_INSERT) < 0) {
 			status = map_ntstatus_from_tdb(tdb);
@@ -273,13 +275,14 @@
 		NTSTATUS status;
 		TDB_DATA key;
 
-		key.dptr = talloc_asprintf(keys, "KEY/%d/%s", i, keys[i]);
+		key.dsize = talloc_asprintf_len(keys, &key.dptr, "KEY/%d/%s",
+						i, keys[i]);
 		if (key.dptr == NULL) {
 			DEBUG(0, ("talloc_asprintf failed\n"));
 			TALLOC_FREE(keys);
 			return NT_STATUS_NO_MEMORY;
 		}
-		key.dsize = strlen(key.dptr)+1;
+		key.dsize += 1;
 
 		if (tdb_delete(tdb, key) < 0) {
 			status = map_ntstatus_from_tdb(tdb);



More information about the samba-cvs mailing list