Rewrite openvzSetUUID.

* src/openvz_conf.c (openvzSetUUID): Rewrite to avoid unchecked
lseek, write, and close as well as a potential file descriptor leak.
This commit is contained in:
Jim Meyering 2008-02-21 18:48:06 +00:00
parent 5fc07cd840
commit 9bcade46df
2 changed files with 14 additions and 12 deletions

View File

@ -1,5 +1,9 @@
Thu Feb 21 19:22:10 CET 2008 Jim Meyering <meyering@redhat.com> Thu Feb 21 19:22:10 CET 2008 Jim Meyering <meyering@redhat.com>
Rewrite openvzSetUUID.
* src/openvz_conf.c (openvzSetUUID): Rewrite to avoid unchecked
lseek, write, and close as well as a potential file descriptor leak.
Handle failed openvzLocateConfDir. Handle failed openvzLocateConfDir.
* src/openvz_conf.c (openvzLocateConfDir, openvzGetVPSUUID): * src/openvz_conf.c (openvzLocateConfDir, openvzGetVPSUUID):
(openvzSetUUID): Don't dereference NULL upon failure. (openvzSetUUID): Don't dereference NULL upon failure.

View File

@ -677,7 +677,6 @@ openvzSetUUID(int vpsid)
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
unsigned char uuid[VIR_UUID_BUFLEN]; unsigned char uuid[VIR_UUID_BUFLEN];
char *conf_dir; char *conf_dir;
int fd, ret;
conf_dir = openvzLocateConfDir(); conf_dir = openvzLocateConfDir();
if (conf_dir == NULL) if (conf_dir == NULL)
@ -685,23 +684,22 @@ openvzSetUUID(int vpsid)
sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid); sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
free(conf_dir); free(conf_dir);
fd = open(conf_file, O_RDWR); if (openvzGetVPSUUID(vpsid, uuidstr))
if(fd == -1)
return -1; return -1;
ret = openvzGetVPSUUID(vpsid, uuidstr); if (uuidstr[0] == 0) {
if(ret == -1) FILE *fp = fopen(conf_file, "a"); /* append */
return -1; if (fp == NULL)
return -1;
if(uuidstr[0] == 0) {
virUUIDGenerate(uuid); virUUIDGenerate(uuid);
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
lseek(fd, 0, SEEK_END); /* Record failure if fprintf or fclose fails,
write(fd, "\n#UUID: ", 8); and be careful always to close the stream. */
write(fd, uuidstr, strlen(uuidstr)); if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0)
write(fd, "\n", 1); + (fclose(fp) == EOF))
close(fd); return -1;
} }
return 0; return 0;