[Libvir] Re: [PATCH] allocate virBuffer* routines
S.Sakamoto
fj0588di at aa.jp.fujitsu.com
Tue Apr 24 12:48:09 UTC 2007
Hi, Daniel
> I can't help thinking this method
> would be better off using the dyn allocated virBuffer* routines instead
> of a static string & snprintf.
Oops, sorry,
virBuffer* routines had entirely slipped my mind.
Buffer is allocated dynamically when I use this.
therefore, I make the patch which virBuffer* routines is in.
Thanks,
Shigeki Sakamoto.
Index: src/internal.h
===================================================================
RCS file: /data/cvs/libvirt/src/internal.h,v
retrieving revision 1.38
diff -u -p -r1.38 internal.h
--- src/internal.h 23 Apr 2007 07:41:23 -0000 1.38
+++ src/internal.h 24 Apr 2007 11:00:50 -0000
@@ -106,11 +106,6 @@ extern "C" {
#define VIR_CONNECT_RO 1
/**
- * buffer size for definition file
- */
-#define VIR_XML_STRING_BUFLEN (1024 + PATH_MAX * 16 + FILENAME_MAX * 16)
-
-/**
* _virConnect:
*
* Internal structure associated to a connection
Index: src/xend_internal.c
===================================================================
RCS file: /data/cvs/libvirt/src/xend_internal.c,v
retrieving revision 1.110
diff -u -p -r1.110 xend_internal.c
--- src/xend_internal.c 23 Apr 2007 07:41:23 -0000 1.110
+++ src/xend_internal.c 24 Apr 2007 11:00:55 -0000
@@ -587,24 +587,33 @@ static int
xend_op_ext2(virConnectPtr xend, const char *path, char *error,
size_t n_error, const char *key, va_list ap)
{
- char ops[VIR_XML_STRING_BUFLEN];
const char *k = key, *v;
- int offset = 0;
+ virBuffer buf;
+ int ret;
+
+ buf.content = malloc(1000);
+ if (buf.content == NULL)
+ return -1;
+ buf.size = 1000;
+ buf.use = 0;
while (k) {
v = va_arg(ap, const char *);
- offset += snprintf(ops + offset, sizeof(ops) - offset, "%s", k);
- offset += snprintf(ops + offset, sizeof(ops) - offset, "%s", "=");
- offset += snprintf(ops + offset, sizeof(ops) - offset, "%s", v);
+ virBufferVSprintf(&buf, "%s", k);
+ virBufferVSprintf(&buf, "%s", "=");
+ virBufferVSprintf(&buf, "%s", v);
k = va_arg(ap, const char *);
if (k)
- offset += snprintf(ops + offset,
- sizeof(ops) - offset, "%s", "&");
+ virBufferVSprintf(&buf, "%s", "&");
}
- return http2unix(xend, xend_post(xend, path, ops, error, n_error));
+ ret = http2unix(xend, xend_post(xend, path, buf.content, error, n_error));
+ if (buf.content != NULL)
+ free(buf.content);
+
+ return ret;
}
More information about the libvir-list
mailing list