[pacman-dev] [PATCH] pacsort: safer sizeof types when allocating memory
--- src/util/pacsort.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/util/pacsort.c b/src/util/pacsort.c index c69ae2f..a652b43 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -80,7 +80,7 @@ static struct buffer_t *buffer_new(size_t initial_size) return NULL; } - buf->mem = calloc(initial_size, sizeof(char)); + buf->mem = calloc(initial_size, sizeof(*(buf->mem))); if(!buf->mem) { free(buf); return NULL; @@ -105,7 +105,7 @@ static void buffer_free(struct buffer_t *buf) static int buffer_grow(struct buffer_t *buffer) { size_t newsz = buffer->maxlen * 2.5; - char* new_mem = realloc(buffer->mem, newsz * sizeof(char)); + char* new_mem = realloc(buffer->mem, newsz * sizeof(*new_mem)); if(!new_mem) { return 1; } @@ -119,12 +119,12 @@ static struct list_t *list_new(size_t initial_size) { struct list_t *list; - list = calloc(1, sizeof(struct list_t)); + list = calloc(1, sizeof(*list)); if(!list) { return NULL; } - list->list = calloc(initial_size, sizeof(char *)); + list->list = calloc(initial_size, sizeof(*(list->list))); if(!list->list) { free(list); return NULL; @@ -138,7 +138,7 @@ static struct list_t *list_new(size_t initial_size) static int list_grow(struct list_t *list) { size_t newsz = list->maxcount * 2.5; - void **new_list = realloc(list->list, newsz * sizeof(char *)); + void **new_list = realloc(list->list, newsz * sizeof(*new_list)); if(!new_list) { return 1; } @@ -202,7 +202,7 @@ static struct input_t *input_new(const char *path, int pathlen) const char *slash; struct input_t *in; - in = calloc(1, sizeof(struct input_t)); + in = calloc(1, sizeof(*in)); if(in == NULL) { return NULL; } -- 2.7.0
On 30/01/16 00:47, Julien Ramseier wrote:
--- src/util/pacsort.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/src/util/pacsort.c b/src/util/pacsort.c index c69ae2f..a652b43 100644 --- a/src/util/pacsort.c +++ b/src/util/pacsort.c @@ -80,7 +80,7 @@ static struct buffer_t *buffer_new(size_t initial_size) return NULL; }
- buf->mem = calloc(initial_size, sizeof(char)); + buf->mem = calloc(initial_size, sizeof(*(buf->mem))); if(!buf->mem) { free(buf); return NULL;
Why is this safer? A
Le 30 janv. 2016 à 10:00, Allan McRae <allan@archlinux.org> a écrit :
Why is this safer?
Safer is maybe not the most appropriate word. This simply prevents mistakes by having to write the correct type twice. Using char* instead of void* is also semantically incorrect: - void **new_list = realloc(list->list, newsz * sizeof(char *)); + void **new_list = realloc(list->list, newsz * sizeof(*new_list)); - Julien
participants (2)
-
Allan McRae
-
Julien Ramseier