summaryrefslogtreecommitdiffstats
path: root/lib/libgeom/geom_getxml.c
diff options
context:
space:
mode:
authorlulf <lulf@FreeBSD.org>2008-07-08 17:34:50 +0000
committerlulf <lulf@FreeBSD.org>2008-07-08 17:34:50 +0000
commit1547b99716941b9b7d256bef62d3fa4955880949 (patch)
tree5b3be73bdbe84255bd046a32bc817c208bfefb52 /lib/libgeom/geom_getxml.c
parente31c8aa8e526df26ff15a5aab776940a6ebaf2aa (diff)
downloadFreeBSD-src-1547b99716941b9b7d256bef62d3fa4955880949.zip
FreeBSD-src-1547b99716941b9b7d256bef62d3fa4955880949.tar.gz
- Simplify the procedure of retrieving XML-data from the kernel.
- Fix a number of potential memory leaks in libgeom related to doing realloc without freeing old pointer if things go wrong. - Fix a number of places in libgeom where malloc and calloc return values were not checked. - Check malloc return value and provide sufficient warning messages when XML parsing fails. PR: kern/83464 Submitted by: Dan Lukes <dan - at - obluda.cz> Approved by: kib (mentor)
Diffstat (limited to 'lib/libgeom/geom_getxml.c')
-rw-r--r--lib/libgeom/geom_getxml.c32
1 files changed, 13 insertions, 19 deletions
diff --git a/lib/libgeom/geom_getxml.c b/lib/libgeom/geom_getxml.c
index 68ff91b..b27696d 100644
--- a/lib/libgeom/geom_getxml.c
+++ b/lib/libgeom/geom_getxml.c
@@ -39,28 +39,22 @@ char *
geom_getxml()
{
char *p;
- size_t l;
- int i;
+ size_t l = 0;
+ int mib[3];
+ size_t sizep;
- l = 1024 * 1024; /* Start big, realloc back */
+ sizep = sizeof(mib) / sizeof(*mib);
+ if (sysctlnametomib("kern.geom.confxml", mib, &sizep) != 0)
+ return (NULL);
+ if (sysctl(mib, sizep, NULL, &l, NULL, 0) != 0)
+ return (NULL);
+ l += 4096;
p = malloc(l);
- if (p) {
- i = sysctlbyname("kern.geom.confxml", p, &l, NULL, 0);
- if (i == 0) {
- p = realloc(p, strlen(p) + 1);
- return (p);
- }
+ if (p == NULL)
+ return (NULL);
+ if (sysctl(mib, sizep, p, &l, NULL, 0) != 0) {
free(p);
- }
- l = 0;
- i = sysctlbyname("kern.geom.confxml", NULL, &l, NULL, 0);
- if (i != 0)
return (NULL);
- p = malloc(l + 4096);
- i = sysctlbyname("kern.geom.confxml", p, &l, NULL, 0);
- if (i == 0) {
- p = realloc(p, strlen(p) + 1);
- return (p);
}
- return (NULL);
+ return (reallocf(p, strlen(p) + 1));
}
OpenPOWER on IntegriCloud