On Mon, Mar 28, 2011 at 6:45 AM, Russell Bryant
<russell(a)russellbryant.net> wrote:
I first started looking at this particular function because of a
warning emitted by valgrind:
==18466== Conditional jump or move depends on uninitialised value(s)
==18466== at 0x3186A4919E: vfprintf (in /lib64/libc-2.13.90.so)
==18466== by 0x3186A71451: vsnprintf (in /lib64/libc-2.13.90.so)
==18466== by 0x3186A51FE1: snprintf (in /lib64/libc-2.13.90.so)
==18466== by 0x4C20697: host_os_get_uuid (in /usr/lib64/libmhost.so.0.0.1)
==18466== by 0x40CC9D: HostAgent::setup(qpid::management::ManagementAgent*) (in
/usr/sbin/matahari-hostd)
==18466== by 0x43A9AA: MatahariAgent::init(int, char**, char const*) (in
/usr/sbin/matahari-hostd)
==18466== by 0x40BB9E: main (in /usr/sbin/matahari-hostd)
The changes are:
1) Instead of open coding a loop looking for an end of line or
the end of the buffer, use strchrnul().
IIRC there was a reason I stopped using that - perhaps it was windows
compatibility but that code is conditional on linux... anyway, ack.
2) The old code ignored a possible allocation failure and would
have crashed on snprintf() in that rare case.
3) The old code would also leak memory if the input file had
more than one line in it.
Signed-off-by: Russell Bryant <russell(a)russellbryant.net>
---
src/lib/utilities.c | 12 +++---------
1 files changed, 3 insertions(+), 9 deletions(-)
diff --git a/src/lib/utilities.c b/src/lib/utilities.c
index cb822fe..01bf26a 100644
--- a/src/lib/utilities.c
+++ b/src/lib/utilities.c
@@ -374,15 +374,9 @@ const char *matahari_uuid(void)
uuid = strdup("unknown");
} else {
- int lpc = 0;
- for (; lpc < data_length; lpc++) {
- switch(buffer[lpc]) {
- case '\0':
- case '\n':
- uuid = malloc(lpc);
- snprintf(uuid, lpc-1, "%s", buffer);
- }
- }
+ char *tmp = strchrnul(buffer, '\n');
+ *tmp = '\0';
+ uuid = strdup(buffer);
}
if(input) {
fclose(input);
--
1.7.4
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari