From: Stefan Schubert <stefan@gefluegelhof-schubert.de>
Date: Fri, 24 Mar 2023 15:14:07 +0100
Subject: Aarch64 gcc13 (#183)

* fixed initializatio error

* fixed buffer overflow

* fixed buffer overflow

(cherry picked from commit 8d086dfc69d4299e55e4844e3573b3a4cf420f19)
---
 lib/getfilecontents.c | 7 +++----
 lib/helpers.c         | 1 +
 lib/libeconf.c        | 2 ++
 lib/libeconf_ext.c    | 1 +
 4 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/lib/getfilecontents.c b/lib/getfilecontents.c
index 94e1db9..f9b1afc 100644
--- a/lib/getfilecontents.c
+++ b/lib/getfilecontents.c
@@ -180,7 +180,7 @@ store (econf_file *ef, const char *group, const char *key,
 
   ef->file_entry[ef->length-1].line_number = line_number;
 
-  ef->file_entry[ef->length-1].quotes |= quotes;
+  ef->file_entry[ef->length-1].quotes = quotes;
 
   if (group)
     ef->file_entry[ef->length-1].group = strdup(group);
@@ -265,11 +265,12 @@ read_file(econf_file *ef, const char *file,
   }
   ef->delimiter = *delim;
 
-  while (fgets(buf, sizeof(buf), kf)) {
+  while (fgets(buf, BUFSIZ-1, kf)) {
     char *p, *name, *data = NULL;
     bool quote_seen = false, delim_seen = false;
     char *org_buf __attribute__ ((__cleanup__(free_buffer))) = strdup(buf);
 
+    buf[BUFSIZ-1] = '\0';
     line++;
     last_scanned_line_nr = line;
 
@@ -410,8 +411,6 @@ read_file(econf_file *ef, const char *file,
       if (!found_delim &&
 	  /* Entry has already been found */
 	  ef->length > 0 &&
-	  /* Value of previous entry is not Null. So delimiter has been found in the previous line */
-	  ef->file_entry[ef->length-1].value != NULL &&
 	  /* The Entry must be the next line. Otherwise it is a new one */
 	  ef->file_entry[ef->length-1].line_number+1 == line)
       {
diff --git a/lib/helpers.c b/lib/helpers.c
index d721727..3e1541a 100644
--- a/lib/helpers.c
+++ b/lib/helpers.c
@@ -47,6 +47,7 @@ void initialize(econf_file *key_file, size_t num) {
   key_file->file_entry[num].value = strdup(KEY_FILE_NULL_VALUE);
   key_file->file_entry[num].comment_before_key = NULL;
   key_file->file_entry[num].comment_after_value = NULL;
+  key_file->file_entry[num].quotes = false;
 }
 
 // Remove whitespace from beginning and end, append string terminator
diff --git a/lib/libeconf.c b/lib/libeconf.c
index d2026ea..0bed98f 100644
--- a/lib/libeconf.c
+++ b/lib/libeconf.c
@@ -509,6 +509,7 @@ econf_err econf_writeFile(econf_file *key_file, const char *save_to_dir,
       char *value_string = buf;
 
       strncpy(buf,key_file->file_entry[i].comment_before_key,BUFSIZ-1);
+      buf[BUFSIZ-1] = '\0';
       while ((line = strsep(&value_string, "\n")) != NULL) {
 	fprintf(kf, "%c%s\n",
 		key_file->comment,
@@ -533,6 +534,7 @@ econf_err econf_writeFile(econf_file *key_file, const char *save_to_dir,
       char *value_string = buf;
 
       strncpy(buf,key_file->file_entry[i].comment_after_value,BUFSIZ-1);
+      buf[BUFSIZ-1] = '\0';
       while ((line = strsep(&value_string, "\n")) != NULL) {
 	fprintf(kf, " %c%s\n",
 		key_file->comment,
diff --git a/lib/libeconf_ext.c b/lib/libeconf_ext.c
index fe3cdf4..137b869 100644
--- a/lib/libeconf_ext.c
+++ b/lib/libeconf_ext.c
@@ -82,6 +82,7 @@ econf_getExtValue(econf_file *kf, const char *group,
 
   if (value_string!=NULL) {
     strncpy(buf,value_string,BUFSIZ-1);
+    buf[BUFSIZ-1] = '\0';
     free(value_string);
     value_string = trim(buf);
 
