From 669f5b1064adc79b46863133eee7c5013487a781 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Wed, 25 Sep 2013 11:28:30 -0400 Subject: [PATCH] [lib-result] mobile_mac_addr as static buffer The mobile_mac_addr field of the struct _owl_result is now a static char buffer instead of being dynamically-allocated. When reading the MAC address, it is also more thoroughly verified. --- libowlps-resultreader/libowlps-resultreader.c | 19 +++++++++++++------ libowlps-resultreader/owlps-resultreader.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/libowlps-resultreader/libowlps-resultreader.c b/libowlps-resultreader/libowlps-resultreader.c index 61813bb..64d8fba 100644 --- a/libowlps-resultreader/libowlps-resultreader.c +++ b/libowlps-resultreader/libowlps-resultreader.c @@ -110,8 +110,16 @@ owl_result* owl_fill_result(char *csv) " string (empty string?)!\n") ; goto error ; } - result->mobile_mac_addr = - strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ; + if (strnlen(csv_field, OWL_ETHER_ADDR_STRLEN) + != OWL_ETHER_ADDR_STRLEN - 1) + { + csv_field[OWL_ETHER_ADDR_STRLEN-1] = '\0' ; + fprintf(stderr, + "The string \"%s\" is not a valid MAC address!\n", + csv_field) ; + goto error ; + } + strncpy(result->mobile_mac_addr, csv_field, OWL_ETHER_ADDR_STRLEN) ; /* Request type */ if (! owl_read_long_field(&csv, CSV_DELIMITER, &longfield)) @@ -528,9 +536,9 @@ void owl_fprint_algorithm_result(FILE *stream, /** - * Frees the memory allocated for an `owl_result`. The `results` and - * `mobile_mac_addr` fields of `result` *must* be defined, either to - * `NULL` or to a valid memory block allocated with `malloc()`. + * Frees the memory allocated for an `owl_result`. The `results` field + * of `result` *must* be defined, either to `NULL` or to a valid memory + * block allocated with `malloc()`. * Note that `result` will not be set to `NULL`. */ void owl_free_result(owl_result *const result) @@ -543,7 +551,6 @@ void owl_free_result(owl_result *const result) result->results = algo->next ; owl_free_algorithm_result(algo) ; } - free(result->mobile_mac_addr) ; free(result) ; } diff --git a/libowlps-resultreader/owlps-resultreader.h b/libowlps-resultreader/owlps-resultreader.h index 05edca1..601cb38 100644 --- a/libowlps-resultreader/owlps-resultreader.h +++ b/libowlps-resultreader/owlps-resultreader.h @@ -96,7 +96,7 @@ typedef struct _owl_algorithm_result owl_algorithm_result ; */ struct _owl_result { - char *mobile_mac_addr ; ///< Mobile's MAC address + char mobile_mac_addr[OWL_ETHER_ADDR_STRLEN] ; ///< Mobile's MAC address uint8_t request_type ; ///< Type of the request /// Local time on the mobile when sending the request owl_timestamp mobile_timestamp ;