From cfd68bb42d49c4fd863d262cf0f10d1d98a73a45 Mon Sep 17 00:00:00 2001 From: Kim Minh Kaplan Date: Fri, 16 Mar 2012 09:48:03 +0000 Subject: [PATCH 1/4] DNSmezzo/packets2postgresql.c: fix type error with hostname variable. --- DNSmezzo/packets2postgresql.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DNSmezzo/packets2postgresql.c b/DNSmezzo/packets2postgresql.c index 286c494..4ea2e48 100644 --- a/DNSmezzo/packets2postgresql.c +++ b/DNSmezzo/packets2postgresql.c @@ -229,7 +229,7 @@ int main(int argc, char *argv[]) { /* Misc. variables */ - char *filename, *ct, *hostname[MAX_NAME], errbuf[PCAP_ERRBUF_SIZE]; + char *filename, *ct, hostname[MAX_NAME], errbuf[PCAP_ERRBUF_SIZE]; pcap_parser_file *inputfile; struct dns_packet *packet; int ch; @@ -320,11 +320,11 @@ main(int argc, char *argv[]) fatal("Cannot prepare statement: %s", PQresultErrorMessage(result)); } } - status = gethostname((char *) hostname, MAX_NAME); + status = gethostname(hostname, sizeof(hostname)); if (status != 0) { fatal("Cannot retrieve host name"); } - file_params[0] = (char *) hostname; + file_params[0] = hostname; file_params[1] = malloc(PATH_MAX + 1); realpath(filename, (char *) file_params[1]); file_params[2] = pcap_datalink_val_to_description(inputfile->datalink); From 64c960ee45c1e5a0b2f4fdc3951b03c34fbb9733 Mon Sep 17 00:00:00 2001 From: Kim Minh Kaplan Date: Fri, 16 Mar 2012 10:58:37 +0000 Subject: [PATCH 2/4] DNSmezzo/packets2postgresql.c: handle realpath(3) errors. Suppress cast. --- DNSmezzo/packets2postgresql.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DNSmezzo/packets2postgresql.c b/DNSmezzo/packets2postgresql.c index 4ea2e48..0f6f533 100644 --- a/DNSmezzo/packets2postgresql.c +++ b/DNSmezzo/packets2postgresql.c @@ -24,6 +24,7 @@ packets. */ /* Standard headers */ +#include #include #include #include @@ -325,8 +326,9 @@ main(int argc, char *argv[]) fatal("Cannot retrieve host name"); } file_params[0] = hostname; - file_params[1] = malloc(PATH_MAX + 1); - realpath(filename, (char *) file_params[1]); + file_params[1] = realpath(filename, NULL); + if (file_params[1] == NULL) + fatal("Can not retrieve realpath for %s: %s", filename, strerror(errno)); file_params[2] = pcap_datalink_val_to_description(inputfile->datalink); file_params[3] = malloc(MAX_INTEGER_WIDTH); sprintf((char *) file_params[3], "%i", inputfile->snaplen); From 0969feee0de1f517f10d157de7d07e90a6971e36 Mon Sep 17 00:00:00 2001 From: Kim Minh Kaplan Date: Fri, 16 Mar 2012 11:18:55 +0000 Subject: [PATCH 3/4] DNSmezzo/packets2postgresql.c: Remove spurious casts to char*. --- DNSmezzo/packets2postgresql.c | 43 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/DNSmezzo/packets2postgresql.c b/DNSmezzo/packets2postgresql.c index 0f6f533..cc5b429 100644 --- a/DNSmezzo/packets2postgresql.c +++ b/DNSmezzo/packets2postgresql.c @@ -330,17 +330,16 @@ main(int argc, char *argv[]) if (file_params[1] == NULL) fatal("Can not retrieve realpath for %s: %s", filename, strerror(errno)); file_params[2] = pcap_datalink_val_to_description(inputfile->datalink); - file_params[3] = malloc(MAX_INTEGER_WIDTH); - sprintf((char *) file_params[3], "%i", inputfile->snaplen); - file_params[4] = malloc(MAX_INTEGER_WIDTH); - sprintf((char *) file_params[4], "%li", (long int) inputfile->size); - file_params[5] = malloc(MAX_TIMESTAMP_WIDTH); + file_params[3] = tmp = malloc(MAX_INTEGER_WIDTH); + sprintf(tmp, "%i", inputfile->snaplen); + file_params[4] = tmp = malloc(MAX_INTEGER_WIDTH); + sprintf(tmp, "%li", (long int) inputfile->size); + file_params[5] = tmp = malloc(MAX_TIMESTAMP_WIDTH); file_creation = *gmtime(&inputfile->creation); - strftime((char *) file_params[5], MAX_TIMESTAMP_WIDTH, ISO_FORMAT, - &file_creation); + strftime(tmp, MAX_TIMESTAMP_WIDTH, ISO_FORMAT, &file_creation); if (maxpackets > 0) { - file_params[6] = malloc(MAX_INTEGER_WIDTH); - sprintf((char *) file_params[6], "%li", maxpackets); + file_params[6] = tmp = malloc(MAX_INTEGER_WIDTH); + sprintf(tmp, "%li", maxpackets); } else { file_params[6] = NULL; } @@ -349,8 +348,8 @@ main(int argc, char *argv[]) if (sampling == NULL) { file_params[7] = NULL; } else { - file_params[7] = malloc(MAX_FLOAT_WIDTH); - sprintf((char *) file_params[7], "%f", 1.0 / (*sampling)); + file_params[7] = tmp = malloc(MAX_FLOAT_WIDTH); + sprintf(tmp, "%f", 1.0 / (*sampling)); } } else { file_params[7] = NULL; @@ -575,25 +574,23 @@ main(int argc, char *argv[]) " - interrupted before the end because max packets read" : ""); free(ct); } - fileend_params[0] = malloc(MAX_INTEGER_WIDTH); - fileend_params[1] = malloc(MAX_INTEGER_WIDTH); - sprintf((char *) fileend_params[0], "%lu", inputfile->packetnum); - sprintf((char *) fileend_params[1], "%lu", inputfile->dnspacketnum); - fileend_params[2] = malloc(MAX_TIMESTAMP_WIDTH); + fileend_params[0] = tmp = malloc(MAX_INTEGER_WIDTH); + sprintf(tmp, "%lu", inputfile->packetnum); + fileend_params[1] = tmp = malloc(MAX_INTEGER_WIDTH); + sprintf(tmp, "%lu", inputfile->dnspacketnum); + fileend_params[2] = tmp = malloc(MAX_TIMESTAMP_WIDTH); date_firstpacket = *gmtime(&inputfile->firstpacket.tv_sec); - strftime((char *) fileend_params[2], MAX_TIMESTAMP_WIDTH, ISO_FORMAT, - &date_firstpacket); + strftime(tmp, MAX_TIMESTAMP_WIDTH, ISO_FORMAT, &date_firstpacket); if (maxpackets == 0) { /* Otherwise, the timestamp of the last packet is * not significant */ - fileend_params[3] = malloc(MAX_TIMESTAMP_WIDTH); + fileend_params[3] = tmp = malloc(MAX_TIMESTAMP_WIDTH); date_lastpacket = *gmtime(&inputfile->lastpacket.tv_sec); - strftime((char *) fileend_params[3], MAX_TIMESTAMP_WIDTH, ISO_FORMAT, - &date_lastpacket); + strftime(tmp, MAX_TIMESTAMP_WIDTH, ISO_FORMAT, &date_lastpacket); } else { fileend_params[3] = NULL; } - fileend_params[4] = malloc(MAX_INTEGER_WIDTH); - sprintf((char *) fileend_params[4], "%i", file_id); + fileend_params[4] = tmp = malloc(MAX_INTEGER_WIDTH); + sprintf(tmp, "%i", file_id); if (!dry_run) { PQclear(result); result = From b5a6442ff15d89bb795568af8a270db24df2c720 Mon Sep 17 00:00:00 2001 From: Kim Minh Kaplan Date: Fri, 16 Mar 2012 11:35:31 +0000 Subject: [PATCH 4/4] DNSmezzo/packets2postgresql.c: use the z length modifier to format size_t values. --- DNSmezzo/packets2postgresql.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DNSmezzo/packets2postgresql.c b/DNSmezzo/packets2postgresql.c index cc5b429..1bd7697 100644 --- a/DNSmezzo/packets2postgresql.c +++ b/DNSmezzo/packets2postgresql.c @@ -333,7 +333,7 @@ main(int argc, char *argv[]) file_params[3] = tmp = malloc(MAX_INTEGER_WIDTH); sprintf(tmp, "%i", inputfile->snaplen); file_params[4] = tmp = malloc(MAX_INTEGER_WIDTH); - sprintf(tmp, "%li", (long int) inputfile->size); + sprintf(tmp, "%zu", inputfile->size); file_params[5] = tmp = malloc(MAX_TIMESTAMP_WIDTH); file_creation = *gmtime(&inputfile->creation); strftime(tmp, MAX_TIMESTAMP_WIDTH, ISO_FORMAT, &file_creation);