From 536bd4261e565f34e7ef8fcb8ca26a0148898ae3 Mon Sep 17 00:00:00 2001 From: gonzo Date: Sun, 5 Feb 2023 12:28:46 +0100 Subject: cleaned up warnings in main.c agenda_entry.* utils.h --- Makefile | 2 +- agenda_entry.c | 26 ++++++++++++++++++-------- agenda_entry.h | 2 ++ main.c | 24 +++++++++--------------- utils.h | 2 ++ 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 8a71ad6..dc27132 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CC=clang --std=gnu99 CC=clang --std=c99 .endif -CFLAGS=-Wall -Wextra -Werror -Weverything -pedantic +CFLAGS=-Wall -Wextra -Werror -Weverything -Wpadded -pedantic LIBS= BLDFLAGS=-g RELFLAGS=-O2 diff --git a/agenda_entry.c b/agenda_entry.c index 8a68353..92fc798 100644 --- a/agenda_entry.c +++ b/agenda_entry.c @@ -14,6 +14,7 @@ entry read_agenda_entry(char *agenda) { char *cursor = agenda; char *lookahead, *tmp; char *nextentry = next_entry(cursor); + unsigned long size; if (nextentry == NULL) nextentry = agenda + strlen(agenda) - 1; @@ -27,17 +28,19 @@ entry read_agenda_entry(char *agenda) { lookahead = strstr(cursor, ":\n"); if (lookahead && (lookahead < nextentry)) { tmp = rstrchr(cursor, lookahead-1, ':'); - agenda_entry.tag = (char *)malloc(lookahead - tmp); - strncpy(agenda_entry.tag, tmp+1, lookahead - tmp - 1); - agenda_entry.tag[lookahead - tmp - 1] = '\0'; // here we're actually modifying lookahead-tmp cause the array is zero indexed + size = (unsigned long)labs(lookahead - tmp); + agenda_entry.tag = (char *)malloc(size); + strncpy(agenda_entry.tag, tmp+1, size - 1); + agenda_entry.tag[size - 1] = '\0'; // here we're actually modifying lookahead-tmp cause the array is zero indexed lookahead = tmp; } else { lookahead = strstr(cursor, "\n"); agenda_entry.tag = NULL; } - agenda_entry.title = (char *)malloc(lookahead - cursor + 1); - strncpy(agenda_entry.title, cursor, lookahead - cursor); - agenda_entry.title[lookahead - cursor] = '\0'; // here we're actually modifying lookahead-cursor cause the array is zero indexed + size = (unsigned long)(lookahead - cursor); + agenda_entry.title = (char *)malloc(size + 1); + strncpy(agenda_entry.title, cursor, size); + agenda_entry.title[size] = '\0'; // here we're actually modifying lookahead-cursor cause the array is zero indexed //find out if there is a deadline and extract it cursor = next_line(cursor); @@ -48,8 +51,9 @@ entry read_agenda_entry(char *agenda) { //there is a deadline or event is scheduled tmp = strstr(lookahead, ">"); lookahead = strstr(lookahead, "<"); - char *date = (char *)malloc(tmp - lookahead + 1); - strncpy(date, lookahead+1, tmp - lookahead - 1); + size = (unsigned long)labs(tmp - lookahead); + char *date = (char *)malloc(size + 1); + strncpy(date, lookahead+1, size - 1); agenda_entry.date = extract_date_from_string(date); free(date); } else { @@ -134,6 +138,12 @@ void merge_entry_array(entry *arr, int halfway, int n) { } } +void destroy_entry_array(entry *arr, int n) { + for (int i = 0; i < n; i++) + destroy_entry(arr[i]); + free(arr); +} + void print_entry(entry e) { char *tmp = print_date_to_string(e.date); printf("%s\n%s\n>%s\n", diff --git a/agenda_entry.h b/agenda_entry.h index ea9ea8a..bd0142d 100644 --- a/agenda_entry.h +++ b/agenda_entry.h @@ -25,5 +25,7 @@ void merge_entry_array(entry *arr, int halfway, int n); void insertion_sort_entry_array(entry *arr, int n); void merge_sort_entry_array(entry *arr, int n); void sort_entry_array(entry *arr, int n); +void destroy_entry_array(entry *arr, int n); + void print_entry(entry e); void destroy_entry(entry e); diff --git a/main.c b/main.c index 8aa45fb..61602ec 100644 --- a/main.c +++ b/main.c @@ -4,17 +4,11 @@ #include "agenda_entry.h" #define BS 1024 //blocksize for buffers -void destroy_entry_array(entry *array, int elements) { - for (int i = 0; i < elements; i++) - destroy_entry(array[i]); - free(array); -} - -char *read_file_to_buffer(char *filename) { - int fd = open(filename, O_RDONLY); +static char *read_file_to_buffer(char *filename) { + int fd = open(filename, O_RDONLY); if (fd < 0) return NULL; // if error opening file, return NULLptr - int bufsize = BS, - bufread = 0; + size_t bufsize = BS; + size_t bufread = 0; char *buf = (char *)malloc(bufsize); char *newbuf; @@ -30,15 +24,15 @@ char *read_file_to_buffer(char *filename) { return buf; } -entry *read_entries_to_array(char *buffer, int *entries) { - int numentries = 5, - idx = 0; +static entry *read_entries_to_array(char *buffer, int *entries) { + unsigned long numentries = 5; + int idx = 0; entry *ret = (entry *)malloc(numentries * sizeof(entry)); char *cursor = buffer; while (cursor) { - if (idx == numentries) { + if ((size_t)idx == numentries) { entry *newret = (entry *)malloc((numentries + 5) * sizeof(entry)); - memcpy(newret, ret, (idx)*sizeof(entry)); + memcpy(newret, ret, ((size_t)idx)*sizeof(entry)); free(ret); ret = newret; numentries += 5; diff --git a/utils.h b/utils.h index eaf4004..fb5e070 100644 --- a/utils.h +++ b/utils.h @@ -1,5 +1,7 @@ #include +char *rstrchr(const char *, char *, char); + char *rstrchr(const char *s, char *start, char c) { char *ret = start; while ((ret > s) && (*ret != c)) ret--; -- cgit v1.2.3