On Mon, Jul 21, 2008 at 03:07:31PM +0400, Evgeniy Sokolov wrote:
On Mon, Jul 21, 2008 at 01:41:27PM +0400, Evgeniy Sokolov wrote:
On Fri, Jul 18, 2008 at 07:16:25PM +0100, Daniel P. Berrange wrote:
Right, let's reuse it, but I notice we are using strtol() in a lot of
places ...openvz driver is not the worse here especially since it has
already an encapsulating function.
I did not know about virStrToLong_i(). Thanks.
Also, I think it would be convenient to create simple function
int
virStrToLongSimple_i(const char *str, int *result)
{
char *endptr;
return virStrToLong_i(str, &endptr, 10, result);
}
If you don't want to deal wit the endptr return value, you can simply
pass in NULL for that param.
from notes:
When END_PTR is NULL, the byte after the final valid digit must be NUL.
I don't want to deal the endptr. But I want to parse strings like
" 123 abc".
Why ? Everywhere else in libvirt treats that as mal-formed input and
rejects it.
Ok. I will follow the same rule.
I rewrited code to parse via virStrToLong_i() function.
patch is attached.
Index: src/openvz_conf.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_conf.c,v
retrieving revision 1.29
diff -u -p -r1.29 openvz_conf.c
--- src/openvz_conf.c 11 Jul 2008 08:56:16 -0000 1.29
+++ src/openvz_conf.c 21 Jul 2008 12:49:14 -0000
@@ -55,6 +55,7 @@
#include "uuid.h"
#include "buf.h"
#include "memory.h"
+#include "util.h"
static char *openvzLocateConfDir(void);
static struct openvz_vm_def *openvzParseXML(virConnectPtr conn, xmlDocPtr xml);
@@ -127,17 +128,11 @@ struct openvz_vm
int
strtoI(const char *str)
{
- int base = 10;
- char *endptr;
int val;
- val = (int) strtol(str, &endptr, base);
+ if (virStrToLong_i(str, NULL, 10, &val) < 0)
+ return 0 ;
- /* Check for various possible errors */
- if ((endptr == str) /* "No digits were found" */
- ||((*endptr != '\0')
- && (*endptr != ' ')) /*"Name contain characters other than integers" */ )
- return 0;
return val;
}
Index: src/openvz_driver.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_driver.c,v
retrieving revision 1.31
diff -u -p -r1.31 openvz_driver.c
--- src/openvz_driver.c 16 Jul 2008 20:42:38 -0000 1.31
+++ src/openvz_driver.c 21 Jul 2008 12:49:14 -0000
@@ -437,7 +437,7 @@ openvzDomainCreateLinux(virConnectPtr co
goto exit;
}
- sscanf(vmdef->name, "%d", &vm->vpsid);
+ vm->vpsid = strtoI(vmdef->name);
vm->status = VIR_DOMAIN_RUNNING;
ovz_driver.num_inactive--;
ovz_driver.num_active++;
@@ -475,7 +475,7 @@ openvzDomainCreate(virDomainPtr dom)
return -1;
}
- sscanf(vm->vmdef->name, "%d", &vm->vpsid);
+ vm->vpsid = strtoI(vm->vmdef->name);
vm->status = VIR_DOMAIN_RUNNING;
ovz_driver.num_inactive --;
ovz_driver.num_active ++;
@@ -636,6 +636,7 @@ static int openvzListDomains(virConnectP
int veid, pid, outfd, errfd;
int ret;
char buf[32];
+ char *endptr;
const char *cmd[] = {VZLIST, "-ovpsid", "-H" , NULL};
ret = virExec(conn, (char **)cmd, &pid, -1, &outfd, &errfd);
@@ -648,7 +649,11 @@ static int openvzListDomains(virConnectP
while(got < nids){
ret = openvz_readline(outfd, buf, 32);
if(!ret) break;
- sscanf(buf, "%d", &veid);
+ if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
+ openvzError(conn, VIR_ERR_INTERNAL_ERROR,
+ _("Could not parse VPS ID %s"), buf);
+ continue;
+ }
ids[got] = veid;
got ++;
}
@@ -667,6 +672,7 @@ static int openvzListDefinedDomains(virC
int veid, pid, outfd, errfd, ret;
char vpsname[OPENVZ_NAME_MAX];
char buf[32];
+ char *endptr;
const char *cmd[] = {VZLIST, "-ovpsid", "-H", "-S", NULL};
/* the -S options lists only stopped domains */
@@ -680,7 +686,11 @@ static int openvzListDefinedDomains(virC
while(got < nnames){
ret = openvz_readline(outfd, buf, 32);
if(!ret) break;
- sscanf(buf, "%d\n", &veid);
+ if (virStrToLong_i(buf, &endptr, 10, &veid) < 0) {
+ openvzError(conn, VIR_ERR_INTERNAL_ERROR,
+ _("Could not parse VPS ID %s"), buf);
+ continue;
+ }
sprintf(vpsname, "%d", veid);
names[got] = strdup(vpsname);
got ++;
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list