Skip to contentSkip to conversation

lib/boilerplate: Allow building with GCC 10.2

PATCH Superseded [email protected] 2 participants 5 messages Nov 11, 2020 – Nov 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/boilerplate: Allow building with GCC 10.2

When trying to compile using GCC 10.2 the following warning / error appeared:

iniparser/iniparser.c: In function ‘iniparser_load’:
iniparser/iniparser.c:616:13: error: ‘sprintf’ arguments 3, 4 may overlap destination object ‘buf’ [-Werror=restrict]

  616 |             sprintf(tmp, "%s:%s", section, key);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

According to POSIX and C99 standard using sprintf() on overlapping buffers may lead to undefined behavior, so using a dedicated buffer for the destination (first argument to sprintf) is necessary.

While at it:
- Fixed some indentations
- Fixed some error handling code paths
Some resources were not properly cleaned up in error conditions
- Fixed buffer initializations: The last bytes were never set to zero

Fixes 67803dce543e ("lib/boilerplate: add iniparser to boilerplate")
---
 lib/boilerplate/iniparser/iniparser.c | 39 ++++++++++++++++++---------
 1 file changed, 27 insertions(+), 12 deletions(-)
lib/boilerplate/iniparser/iniparser.c +27 -12
index 5b2094a00..b79f965a6 100644
@@ -551,25 +551,37 @@ dictionary * iniparser_load(const char * ininame)
     dict = dictionary_new(0) ;
     if (!dict) {
         fclose(in);
-	errno = ENOMEM;
+        errno = ENOMEM;
         return NULL ;
     }
 
-    buf = malloc((ASCIILINESZ+1) * 5);
+    buf = malloc((ASCIILINESZ+1) * 4);
     if (buf == NULL) {
-	    errno = -ENOMEM;
-	    return NULL;
+            dictionary_del(dict);
+            fclose(in);
+            errno = ENOMEM;
+            return NULL;
     }
+
+    tmp = malloc(ASCIILINESZ + 1);
+    if (!tmp) {
+            free(buf);
+            dictionary_del(dict);
+            fclose(in);
+            errno = ENOMEM;
+            return NULL;
+    }
+
     line = buf;
     section = line + ASCIILINESZ + 1;
     key = section + ASCIILINESZ + 1;
-    tmp = key + ASCIILINESZ + 1;
-    val = tmp + ASCIILINESZ + 1;
+    val = key + ASCIILINESZ + 1;
 
-    memset(line,    0, ASCIILINESZ);
-    memset(section, 0, ASCIILINESZ);
-    memset(key,     0, ASCIILINESZ);
-    memset(val,     0, ASCIILINESZ);
+    memset(line,    0, ASCIILINESZ + 1);
+    memset(section, 0, ASCIILINESZ + 1);
+    memset(key,     0, ASCIILINESZ + 1);
+    memset(val,     0, ASCIILINESZ + 1);
+    memset(tmp,     0, ASCIILINESZ + 1);
     last=0 ;
 
     while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
@@ -585,8 +597,9 @@ dictionary * iniparser_load(const char * ininame)
 #endif
             dictionary_del(dict);
             fclose(in);
-	    free(buf);
-	    errno = EINVAL;
+            free(buf);
+            free(tmp);
+            errno = EINVAL;
             return NULL ;
         }
         /* Get rid of \n and spaces at end of line */
@@ -613,6 +626,7 @@ dictionary * iniparser_load(const char * ininame)
             break ;
 
             case LINE_VALUE:
+            /* Already checked for overflows, see above */
             sprintf(tmp, "%s:%s", section, key);
             errs = dictionary_set(dict, tmp, val) ;
             break ;
@@ -642,6 +656,7 @@ dictionary * iniparser_load(const char * ininame)
     }
     fclose(in);
     free(buf);
+    free(tmp);
     if (errs) {
         dictionary_del(dict);
         dict = NULL ;
Signature · 1 line
2.26.2
#1 lore raw

Interesting corner of our code base...

Quoted 8 lines · florian.bezdeka--- via Xenomai

When trying to compile using GCC 10.2 the following warning / error appeared:

iniparser/iniparser.c: In function ‘iniparser_load’:
iniparser/iniparser.c:616:13: error: ‘sprintf’ arguments 3, 4 may overlap destination object ‘buf’ [-Werror=restrict]

  616 |             sprintf(tmp, "%s:%s", section, key);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Strange, cannot reproduce this report here with

gcc-10 (SUSE Linux) 10.2.1 20200825 [revision
c0746a1beb1ba073c7981eb09f55b3d993b32e5c]

even when explicitly adding -Wrestrict.

Quoted 10 lines

According to POSIX and C99 standard using sprintf() on overlapping buffers may lead to undefined behavior, so using a dedicated buffer for the destination (first argument to sprintf) is necessary.

While at it:
- Fixed some indentations
- Fixed some error handling code paths
Some resources were not properly cleaned up in error conditions
- Fixed buffer initializations: The last bytes were never set to zero

We likely first of all want a resync with
https://github.com/ndevilla/iniparser. If there is an upstream issue, maybe fix there first.

Quoted 29 lines
Fixes 67803dce543e ("lib/boilerplate: add iniparser to boilerplate")
---
 lib/boilerplate/iniparser/iniparser.c | 39 ++++++++++++++++++---------
 1 file changed, 27 insertions(+), 12 deletions(-)
lib/boilerplate/iniparser/iniparser.c +6 -4
index 5b2094a00..b79f965a6 100644
@@ -551,25 +551,37 @@ dictionary * iniparser_load(const char * ininame)
     dict = dictionary_new(0) ;
     if (!dict) {
         fclose(in);
-	errno = ENOMEM;
+        errno = ENOMEM;
         return NULL ;
     }
 
-    buf = malloc((ASCIILINESZ+1) * 5);
+    buf = malloc((ASCIILINESZ+1) * 4);
     if (buf == NULL) {
-	    errno = -ENOMEM;
-	    return NULL;
+            dictionary_del(dict);
+            fclose(in);
+            errno = ENOMEM;
+            return NULL;
     }

lib/boilerplate/iniparser/iniparser.c dictionary * iniparser_load(const char * ininame) · line 574

Quoted 10 lines
lib/boilerplate/iniparser/iniparser.c +10 -0
+
+    tmp = malloc(ASCIILINESZ + 1);
+    if (!tmp) {
+            free(buf);
+            dictionary_del(dict);
+            fclose(in);
+            errno = ENOMEM;
+            return NULL;
+    }
+

This split-up is just silencing the compiler, right? It does no take away the possibility that we overrun the target buffer in sprintf (section+key > ASCIILINESZ). Upstream seems to have addressed that.

But it also does not use snprintf yet. Likely with the argument that buffer sizes are under full control.

Quoted 41 lines
lib/boilerplate/iniparser/iniparser.c +10 -8
     line = buf;
     section = line + ASCIILINESZ + 1;
     key = section + ASCIILINESZ + 1;
-    tmp = key + ASCIILINESZ + 1;
-    val = tmp + ASCIILINESZ + 1;
+    val = key + ASCIILINESZ + 1;
 
-    memset(line,    0, ASCIILINESZ);
-    memset(section, 0, ASCIILINESZ);
-    memset(key,     0, ASCIILINESZ);
-    memset(val,     0, ASCIILINESZ);
+    memset(line,    0, ASCIILINESZ + 1);
+    memset(section, 0, ASCIILINESZ + 1);
+    memset(key,     0, ASCIILINESZ + 1);
+    memset(val,     0, ASCIILINESZ + 1);
+    memset(tmp,     0, ASCIILINESZ + 1);
     last=0 ;
 
     while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
@@ -585,8 +597,9 @@ dictionary * iniparser_load(const char * ininame)
 #endif
             dictionary_del(dict);
             fclose(in);
-	    free(buf);
-	    errno = EINVAL;
+            free(buf);
+            free(tmp);
+            errno = EINVAL;
             return NULL ;
         }
         /* Get rid of \n and spaces at end of line */
@@ -613,6 +626,7 @@ dictionary * iniparser_load(const char * ininame)
             break ;
 
             case LINE_VALUE:
+            /* Already checked for overflows, see above */
             sprintf(tmp, "%s:%s", section, key);
             errs = dictionary_set(dict, tmp, val) ;
             break ;

lib/boilerplate/iniparser/iniparser.c dictionary * iniparser_load(const char * ininame) · line 662

Quoted 7 lines
lib/boilerplate/iniparser/iniparser.c +1 -0
@@ -642,6 +656,7 @@ dictionary * iniparser_load(const char * ininame)
     }
     fclose(in);
     free(buf);
+    free(tmp);
     if (errs) {
         dictionary_del(dict);
         dict = NULL ;

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#2 lore raw
Quoted 13 lines · Jan Kiszka

Interesting corner of our code base...

Quoted 9 lines · florian.bezdeka--- via Xenomai

When trying to compile using GCC 10.2 the following warning / error appeared:

iniparser/iniparser.c: In function ‘iniparser_load’:
iniparser/iniparser.c:616:13: error: ‘sprintf’ arguments 3, 4 may overlap destination object ‘buf’ [-Werror=restrict]

  616 |             sprintf(tmp, "%s:%s", section, key);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 4 lines

Strange, cannot reproduce this report here with

gcc-10 (SUSE Linux) 10.2.1 20200825 [revision
c0746a1beb1ba073c7981eb09f55b3d993b32e5c]

Here:
gcc version 10.2.1 20201016 (Red Hat 10.2.1-6) (GCC)

Quoted 16 lines

even when explicitly adding -Wrestrict.

Quoted 5 lines

According to POSIX and C99 standard using sprintf() on overlapping buffers may lead to undefined behavior, so using a dedicated buffer for
the destination (first argument to sprintf) is necessary.

Quoted 7 lines

While at it:
- Fixed some indentations
- Fixed some error handling code paths
Some resources were not properly cleaned up in error conditions
- Fixed buffer initializations: The last bytes were never set to
zero

Quoted 3 lines

We likely first of all want a resync with
https://github.com/ndevilla/iniparser. If there is an upstream issue, maybe fix there first.

Oh, sorry... I did not recognize that we have an upstream involved. I will retry with latest upstream version.

Quoted 40 lines
Quoted 40 lines
Fixes 67803dce543e ("lib/boilerplate: add iniparser to

boilerplate")

---
 lib/boilerplate/iniparser/iniparser.c | 39 ++++++++++++++++++-----
----
 1 file changed, 27 insertions(+), 12 deletions(-)
lib/boilerplate/iniparser/iniparser.c +12 -4
index 5b2094a00..b79f965a6 100644
@@ -551,25 +551,37 @@ dictionary * iniparser_load(const char *
 ininame)
     dict = dictionary_new(0) ;
     if (!dict) {
         fclose(in);
-	errno = ENOMEM;
+        errno = ENOMEM;
         return NULL ;
     }
 
-    buf = malloc((ASCIILINESZ+1) * 5);
+    buf = malloc((ASCIILINESZ+1) * 4);
     if (buf == NULL) {
-	    errno = -ENOMEM;
-	    return NULL;
+            dictionary_del(dict);
+            fclose(in);
+            errno = ENOMEM;
+            return NULL;
     }
+
+    tmp = malloc(ASCIILINESZ + 1);
+    if (!tmp) {
+            free(buf);
+            dictionary_del(dict);
+            fclose(in);
Quoted 8 lines

lib/boilerplate/iniparser/iniparser.c dictionary * iniparser_load(const char * ininame) · line 574

Quoted 4 lines
lib/boilerplate/iniparser/iniparser.c +4 -0
+            errno = ENOMEM;
+            return NULL;
+    }
+

This split-up is just silencing the compiler, right? It does no take away the possibility that we overrun the target buffer in sprintf (section+key > ASCIILINESZ). Upstream seems to have addressed that.

Silencing the compiler but overflow is handled (so taken care of) in iniparser_line. Upstream switched to buffers on the stack in the meantime, so error handling is simplified a bit.

Quoted 57 lines

But it also does not use snprintf yet. Likely with the argument that buffer sizes are under full control.

Quoted 49 lines
lib/boilerplate/iniparser/iniparser.c +6 -6
     line = buf;
     section = line + ASCIILINESZ + 1;
     key = section + ASCIILINESZ + 1;
-    tmp = key + ASCIILINESZ + 1;
-    val = tmp + ASCIILINESZ + 1;
+    val = key + ASCIILINESZ + 1;
 
-    memset(line,    0, ASCIILINESZ);
-    memset(section, 0, ASCIILINESZ);
-    memset(key,     0, ASCIILINESZ);
-    memset(val,     0, ASCIILINESZ);
+    memset(line,    0, ASCIILINESZ + 1);
+    memset(section, 0, ASCIILINESZ + 1);
+    memset(key,     0, ASCIILINESZ + 1);
+    memset(val,     0, ASCIILINESZ + 1);
+    memset(tmp,     0, ASCIILINESZ + 1);
     last=0 ;
 
     while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
lib/boilerplate/iniparser/iniparser.c +3 -2
@@ -585,8 +597,9 @@ dictionary * iniparser_load(const char *
 ininame)
 #endif
             dictionary_del(dict);
             fclose(in);
-	    free(buf);
-	    errno = EINVAL;
+            free(buf);
+            free(tmp);
+            errno = EINVAL;
             return NULL ;
         }
         /* Get rid of \n and spaces at end of line */
lib/boilerplate/iniparser/iniparser.c +1 -0
@@ -613,6 +626,7 @@ dictionary * iniparser_load(const char *
 ininame)
             break ;
 
             case LINE_VALUE:
+            /* Already checked for overflows, see above */
             sprintf(tmp, "%s:%s", section, key);
             errs = dictionary_set(dict, tmp, val) ;
             break ;
lib/boilerplate/iniparser/iniparser.c +1 -0
@@ -642,6 +656,7 @@ dictionary * iniparser_load(const char *
 ininame)
     }
     fclose(in);
     free(buf);
+    free(tmp);
     if (errs) {
         dictionary_del(dict);
Quoted 2 lines
         dict = NULL ;

Jan

#3 lore raw

PATCH v2 1/1 lib/boilerplate/iniparser: Allow building with GCC 10.2 2020101

From: Florian Bezdeka <[email protected]>

Updating to upstream revision f858275f7f307eecba84c2f5429483f9f28007f8.

The reason for updating was the following compiler error when trying to compile with GCC 10.2 10.2.1 20201016. As it turned out the problem was already addressed upstream:

iniparser/iniparser.c: In function ‘iniparser_load’:
iniparser/iniparser.c:616:13: error: ‘sprintf’ arguments 3, 4 may overlap destination object ‘buf’ [-Werror=restrict]

   616 |             sprintf(tmp, "%s:%s", section, key);
       |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I reviewed especially the API changes. Most of them are cleanups only but two things should be pointed out:

- The type of the size field of struct _dictionary_ changed from int to ssize_t. The only user of this struct is lib/analogy/calibration.c which uses this structure for internal things only. It is never exposed to any public API so updating is OK and fully backward compatible.

- dictionary_new changed its signature
from dictionary_new(int size)
to dictionary_new(size_t size).
This function is not part of any public API. So updating does not break backward compatibility.

---
 lib/boilerplate/iniparser/dictionary.c | 409 ++++++++++----------
 lib/boilerplate/iniparser/dictionary.h |  43 ++-
 lib/boilerplate/iniparser/iniparser.c  | 491 +++++++++++++++++--------
 lib/boilerplate/iniparser/iniparser.h  | 131 +++++--
 4 files changed, 646 insertions(+), 428 deletions(-)
lib/boilerplate/iniparser/dictionary.c +192 -217
index 5299b77ed..cb7ccd49e 100644
@@ -1,10 +1,8 @@
 /*-------------------------------------------------------------------------*/
 /**
-   @file	dictionary.c
-   @author	N. Devillard
-   @date	Sep 2007
-   @version	$Revision: 1.27 $
-   @brief	Implements a dictionary for string variables.
+   @file    dictionary.c
+   @author  N. Devillard
+   @brief   Implements a dictionary for string variables.
 
    This module implements a simple dictionary object, i.e. a list
    of string/string associations. This object is useful to store e.g.
@@ -12,12 +10,8 @@
 */
 /*--------------------------------------------------------------------------*/
 
-/*
-	$Id: dictionary.c,v 1.27 2007-11-23 21:39:18 ndevilla Exp $
-	$Revision: 1.27 $
-*/
 /*---------------------------------------------------------------------------
-   								Includes
+                                Includes
  ---------------------------------------------------------------------------*/
 #include "dictionary.h"
 
@@ -27,33 +21,18 @@
 #include <unistd.h>
 
 /** Maximum value size for integers and doubles. */
-#define MAXVALSZ	1024
+#define MAXVALSZ    1024
 
 /** Minimal allocated number of entries in a dictionary */
-#define DICTMINSZ	128
+#define DICTMINSZ   128
 
 /** Invalid key token */
 #define DICT_INVALID_KEY    ((char*)-1)
 
 /*---------------------------------------------------------------------------
-  							Private functions
+                            Private functions
  ---------------------------------------------------------------------------*/
 
-/* Doubles the allocated size associated to a pointer */
-/* 'size' is the current allocated size. */
-static void * mem_double(void * ptr, int size)
-{
-    void * newptr ;
- 
-    newptr = calloc(2*size, 1);
-    if (newptr==NULL) {
-        return NULL ;
-    }
-    memcpy(newptr, ptr, size);
-    free(ptr);
-    return newptr ;
-}
-
 /*-------------------------------------------------------------------------*/
 /**
   @brief    Duplicate a string
@@ -67,23 +46,68 @@ static void * mem_double(void * ptr, int size)
 static char * xstrdup(const char * s)
 {
     char * t ;
+    size_t len ;
     if (!s)
         return NULL ;
-    t = malloc(strlen(s)+1) ;
+
+    len = strlen(s) + 1 ;
+    t = (char*) malloc(len) ;
     if (t) {
-        strcpy(t,s);
+        memcpy(t, s, len) ;
     }
     return t ;
 }
 
+/*-------------------------------------------------------------------------*/
+/**
+  @brief    Double the size of the dictionary
+  @param    d Dictionary to grow
+  @return   This function returns non-zero in case of failure
+ */
+/*--------------------------------------------------------------------------*/
+static int dictionary_grow(dictionary * d)
+{
+    char        ** new_val ;
+    char        ** new_key ;
+    unsigned     * new_hash ;
+
+    new_val  = (char**) calloc(d->size * 2, sizeof *d->val);
+    new_key  = (char**) calloc(d->size * 2, sizeof *d->key);
+    new_hash = (unsigned*) calloc(d->size * 2, sizeof *d->hash);
+    if (!new_val || !new_key || !new_hash) {
+        /* An allocation failed, leave the dictionary unchanged */
+        if (new_val)
+            free(new_val);
+        if (new_key)
+            free(new_key);
+        if (new_hash)
+            free(new_hash);
+        return -1 ;
+    }
+    /* Initialize the newly allocated space */
+    memcpy(new_val, d->val, d->size * sizeof(char *));
+    memcpy(new_key, d->key, d->size * sizeof(char *));
+    memcpy(new_hash, d->hash, d->size * sizeof(unsigned));
+    /* Delete previous data */
+    free(d->val);
+    free(d->key);
+    free(d->hash);
+    /* Actually update the dictionary */
+    d->size *= 2 ;
+    d->val = new_val;
+    d->key = new_key;
+    d->hash = new_hash;
+    return 0 ;
+}
+
 /*---------------------------------------------------------------------------
-  							Function codes
+                            Function codes
  ---------------------------------------------------------------------------*/
 /*-------------------------------------------------------------------------*/
 /**
-  @brief	Compute the hash key for a string.
-  @param	key		Character string to use for key.
-  @return	1 unsigned int on at least 32 bits.
+  @brief    Compute the hash key for a string.
+  @param    key     Character string to use for key.
+  @return   1 unsigned int on at least 32 bits.
 
   This hash function has been taken from an Article in Dr Dobbs Journal.
   This is normally a collision-free function, distributing keys evenly.
@@ -93,84 +117,88 @@ static char * xstrdup(const char * s)
 /*--------------------------------------------------------------------------*/
 unsigned dictionary_hash(const char * key)
 {
-	int			len ;
-	unsigned	hash ;
-	int			i ;
-
-	len = strlen(key);
-	for (hash=0, i=0 ; i<len ; i++) {
-		hash += (unsigned)key[i] ;
-		hash += (hash<<10);
-		hash ^= (hash>>6) ;
-	}
-	hash += (hash <<3);
-	hash ^= (hash >>11);
-	hash += (hash <<15);
-	return hash ;
+    size_t      len ;
+    unsigned    hash ;
+    size_t      i ;
+
+    if (!key)
+        return 0 ;
+
+    len = strlen(key);
+    for (hash=0, i=0 ; i<len ; i++) {
+        hash += (unsigned)key[i] ;
+        hash += (hash<<10);
+        hash ^= (hash>>6) ;
+    }
+    hash += (hash <<3);
+    hash ^= (hash >>11);
+    hash += (hash <<15);
+    return hash ;
 }
 
 /*-------------------------------------------------------------------------*/
 /**
-  @brief	Create a new dictionary object.
-  @param	size	Optional initial size of the dictionary.
-  @return	1 newly allocated dictionary objet.
+  @brief    Create a new dictionary object.
+  @param    size    Optional initial size of the dictionary.
+  @return   1 newly allocated dictionary objet.
 
   This function allocates a new dictionary object of given size and returns
   it. If you do not know in advance (roughly) the number of entries in the
   dictionary, give size=0.
  */
-/*--------------------------------------------------------------------------*/
-dictionary * dictionary_new(int size)
+/*-------------------------------------------------------------------------*/
+dictionary * dictionary_new(size_t size)
 {
-	dictionary	*	d ;
-
-	/* If no size was specified, allocate space for DICTMINSZ */
-	if (size<DICTMINSZ) size=DICTMINSZ ;
-
-	if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) {
-		return NULL;
-	}
-	d->size = size ;
-	d->val  = (char **)calloc(size, sizeof(char*));
-	d->key  = (char **)calloc(size, sizeof(char*));
-	d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
-	return d ;
+    dictionary  *   d ;
+
+    /* If no size was specified, allocate space for DICTMINSZ */
+    if (size<DICTMINSZ) size=DICTMINSZ ;
+
+    d = (dictionary*) calloc(1, sizeof *d) ;
+
+    if (d) {
+        d->size = size ;
+        d->val  = (char**) calloc(size, sizeof *d->val);
+        d->key  = (char**) calloc(size, sizeof *d->key);
+        d->hash = (unsigned*) calloc(size, sizeof *d->hash);
+    }
+    return d ;
 }
 
 /*-------------------------------------------------------------------------*/
 /**
-  @brief	Delete a dictionary object
-  @param	d	dictionary object to deallocate.
-  @return	void
+  @brief    Delete a dictionary object
+  @param    d   dictionary object to deallocate.
+  @return   void
 
   Deallocate a dictionary object and all memory associated to it.
  */
 /*--------------------------------------------------------------------------*/
 void dictionary_del(dictionary * d)
 {
-	int		i ;
-
-	if (d==NULL) return ;
-	for (i=0 ; i<d->size ; i++) {
-		if (d->key[i]!=NULL)
-			free(d->key[i]);
-		if (d->val[i]!=NULL)
-			free(d->val[i]);
-	}
-	free(d->val);
-	free(d->key);
-	free(d->hash);
-	free(d);
-	return ;
+    ssize_t  i ;
+
+    if (d==NULL) return ;
+    for (i=0 ; i<d->size ; i++) {
+        if (d->key[i]!=NULL)
+            free(d->key[i]);
+        if (d->val[i]!=NULL)
+            free(d->val[i]);
+    }
+    free(d->val);
+    free(d->key);
+    free(d->hash);
+    free(d);
+    return ;
 }
 
 /*-------------------------------------------------------------------------*/
 /**
-  @brief	Get a value from a dictionary.
-  @param	d		dictionary object to search.
-  @param	key		Key to look for in the dictionary.
+  @brief    Get a value from a dictionary.
+  @param    d       dictionary object to search.
+  @param    key     Key to look for in the dictionary.
   @param    def     Default value to return if key not found.
-  @return	1 pointer to internally allocated character string.
+  @return   1 pointer to internally allocated character string.
 
   This function locates a key in a dictionary and returns a pointer to its
   value, or the passed 'def' pointer if no such key can be found in
@@ -178,24 +206,24 @@ void dictionary_del(dictionary * d)
   dictionary object, you should not try to free it or modify it.
  */
 /*--------------------------------------------------------------------------*/
-const char * dictionary_get(dictionary * d, const char * key, const char * def)
+const char * dictionary_get(const dictionary * d, const char * key, const char * def)
 {
-	unsigned	hash ;
-	int			i ;
+    unsigned    hash ;
+    ssize_t      i ;
 
-	hash = dictionary_hash(key);
-	for (i=0 ; i<d->size ; i++) {
+    hash = dictionary_hash(key);
+    for (i=0 ; i<d->size ; i++) {
         if (d->key[i]==NULL)
             continue ;
         /* Compare hash */
-		if (hash==d->hash[i]) {
+        if (hash==d->hash[i]) {
             /* Compare string, to avoid hash collisions */
             if (!strcmp(key, d->key[i])) {
-				return d->val[i] ;
-			}
-		}
-	}
-	return def ;
+                return d->val[i] ;
+            }
+        }
+    }
+    return def ;
 }
 
 /*-------------------------------------------------------------------------*/
@@ -226,66 +254,57 @@ const char * dictionary_get(dictionary * d, const char * key, const char * def)
 /*--------------------------------------------------------------------------*/
 int dictionary_set(dictionary * d, const char * key, const char * val)
 {
-	int			i ;
-	unsigned	hash ;
-
-	if (d==NULL || key==NULL) return -1 ;
-	
-	/* Compute hash for this key */
-	hash = dictionary_hash(key) ;
-	/* Find if value is already in dictionary */
-	if (d->n>0) {
-		for (i=0 ; i<d->size ; i++) {
+    ssize_t         i ;
+    unsigned       hash ;
+
+    if (d==NULL || key==NULL) return -1 ;
+
+    /* Compute hash for this key */
+    hash = dictionary_hash(key) ;
+    /* Find if value is already in dictionary */
+    if (d->n>0) {
+        for (i=0 ; i<d->size ; i++) {
             if (d->key[i]==NULL)
                 continue ;
-			if (hash==d->hash[i]) { /* Same hash value */
-				if (!strcmp(key, d->key[i])) {	 /* Same key */
-					/* Found a value: modify and return */
-					if (d->val[i]!=NULL)
-						free(d->val[i]);
-                    d->val[i] = val ? xstrdup(val) : NULL ;
+            if (hash==d->hash[i]) { /* Same hash value */
+                if (!strcmp(key, d->key[i])) {   /* Same key */
+                    /* Found a value: modify and return */
+                    if (d->val[i]!=NULL)
+                        free(d->val[i]);
+                    d->val[i] = (val ? xstrdup(val) : NULL);
                     /* Value has been modified: return */
-					return 0 ;
-				}
-			}
-		}
-	}
-	/* Add a new value */
-	/* See if dictionary needs to grow */
-	if (d->n==d->size) {
-
-		/* Reached maximum size: reallocate dictionary */
-		d->val  = (char **)mem_double(d->val,  d->size * sizeof(char*)) ;
-		d->key  = (char **)mem_double(d->key,  d->size * sizeof(char*)) ;
-		d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
-        if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
-            /* Cannot grow dictionary */
-            return -1 ;
+                    return 0 ;
+                }
+            }
         }
-		/* Double size */
-		d->size *= 2 ;
-	}
+    }
+    /* Add a new value */
+    /* See if dictionary needs to grow */
+    if (d->n==d->size) {
+        /* Reached maximum size: reallocate dictionary */
+        if (dictionary_grow(d) != 0)
+            return -1;
+    }
 
-    /* Insert key in the first empty slot */
-    for (i=0 ; i<d->size ; i++) {
-        if (d->key[i]==NULL) {
-            /* Add key here */
-            break ;
-        }
+    /* Insert key in the first empty slot. Start at d->n and wrap at
+       d->size. Because d->n < d->size this will necessarily
+       terminate. */
+    for (i=d->n ; d->key[i] ; ) {
+        if(++i == d->size) i = 0;
     }
-	/* Copy key */
-	d->key[i]  = xstrdup(key);
-    d->val[i]  = val ? xstrdup(val) : NULL ;
-	d->hash[i] = hash;
-	d->n ++ ;
-	return 0 ;
+    /* Copy key */
+    d->key[i]  = xstrdup(key);
+    d->val[i]  = (val ? xstrdup(val) : NULL) ;
+    d->hash[i] = hash;
+    d->n ++ ;
+    return 0 ;
 }
 
 /*-------------------------------------------------------------------------*/
 /**
-  @brief	Delete a key in a dictionary
-  @param	d		dictionary object to modify.
-  @param	key		Key to remove.
+  @brief    Delete a key in a dictionary
+  @param    d       dictionary object to modify.
+  @param    key     Key to remove.
   @return   void
 
   This function deletes a key in a dictionary. Nothing is done if the
@@ -294,26 +313,26 @@ int dictionary_set(dictionary * d, const char * key, const char * val)
 /*--------------------------------------------------------------------------*/
 void dictionary_unset(dictionary * d, const char * key)
 {
-	unsigned	hash ;
-	int			i ;
+    unsigned    hash ;
+    ssize_t      i ;
 
-	if (key == NULL) {
-		return;
-	}
+    if (key == NULL || d == NULL) {
+        return;
+    }
 
-	hash = dictionary_hash(key);
-	for (i=0 ; i<d->size ; i++) {
+    hash = dictionary_hash(key);
+    for (i=0 ; i<d->size ; i++) {
         if (d->key[i]==NULL)
             continue ;
         /* Compare hash */
-		if (hash==d->hash[i]) {
+        if (hash==d->hash[i]) {
             /* Compare string, to avoid hash collisions */
             if (!strcmp(key, d->key[i])) {
                 /* Found key */
                 break ;
-			}
-		}
-	}
+            }
+        }
+    }
     if (i>=d->size)
         /* Key not found */
         return ;
@@ -331,75 +350,31 @@ void dictionary_unset(dictionary * d, const char * key)
 
 /*-------------------------------------------------------------------------*/
 /**
-  @brief	Dump a dictionary to an opened file pointer.
-  @param	d	Dictionary to dump
-  @param	out	Opened file pointer.
-  @return	void
+  @brief    Dump a dictionary to an opened file pointer.
+  @param    d   Dictionary to dump
+  @param    f   Opened file pointer.
+  @return   void
 
   Dumps a dictionary onto an opened file pointer. Key pairs are printed out
   as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
   output file pointers.
  */
 /*--------------------------------------------------------------------------*/
-void dictionary_dump(dictionary * d, FILE * out)
+void dictionary_dump(const dictionary * d, FILE * out)
 {
-	int		i ;
-
-	if (d==NULL || out==NULL) return ;
-	if (d->n<1) {
-		fprintf(out, "empty dictionary\n");
-		return ;
-	}
-	for (i=0 ; i<d->size ; i++) {
+    ssize_t  i ;
+
+    if (d==NULL || out==NULL) return ;
+    if (d->n<1) {
+        fprintf(out, "empty dictionary\n");
+        return ;
+    }
+    for (i=0 ; i<d->size ; i++) {
         if (d->key[i]) {
             fprintf(out, "%20s\t[%s]\n",
                     d->key[i],
                     d->val[i] ? d->val[i] : "UNDEF");
         }
-	}
-	return ;
-}
-
-
-/* Test code */
-#ifdef TESTDIC
-#define NVALS 20000
-int main(int argc, char *argv[])
-{
-	dictionary	*	d ;
-	char	*	val ;
-	int			i ;
-	char		cval[90] ;
-
-	/* Allocate dictionary */
-	printf("allocating...\n");
-	d = dictionary_new(0);
-	
-	/* Set values in dictionary */
-	printf("setting %d values...\n", NVALS);
-	for (i=0 ; i<NVALS ; i++) {
-		sprintf(cval, "%04d", i);
-		dictionary_set(d, cval, "salut");
-	}
-	printf("getting %d values...\n", NVALS);
-	for (i=0 ; i<NVALS ; i++) {
-		sprintf(cval, "%04d", i);
-		val = dictionary_get(d, cval, DICT_INVALID_KEY);
-		if (val==DICT_INVALID_KEY) {
-			printf("cannot get value for key [%s]\n", cval);
-		}
-	}
-    printf("unsetting %d values...\n", NVALS);
-	for (i=0 ; i<NVALS ; i++) {
-		sprintf(cval, "%04d", i);
-		dictionary_unset(d, cval);
-	}
-    if (d->n != 0) {
-        printf("error deleting values\n");
     }
-	printf("deallocating...\n");
-	dictionary_del(d);
-	return 0 ;
+    return ;
 }
-#endif
-/* vim: set ts=4 et sw=4 tw=75 */
lib/boilerplate/iniparser/dictionary.h +21 -22

104 lines not on this page · raw message

lib/boilerplate/iniparser/iniparser.c +329 -162

727 lines not on this page · raw message

lib/boilerplate/iniparser/iniparser.h +104 -27

222 lines not on this page · raw message

Signature · 1 line
2.26.2
#4 lore raw

Re: [PATCH v2] lib/boilerplate/iniparser: Allow building with GCC 10.2 2020101

Quoted 29 lines · Florian (T RDA IOT SES-DE)

From: Florian Bezdeka <[email protected]>

Updating to upstream revision f858275f7f307eecba84c2f5429483f9f28007f8.

The reason for updating was the following compiler error when trying to compile with GCC 10.2 10.2.1 20201016. As it turned out the problem was already addressed upstream:

iniparser/iniparser.c: In function ‘iniparser_load’:
iniparser/iniparser.c:616:13: error: ‘sprintf’ arguments 3, 4 may overlap destination object ‘buf’ [-Werror=restrict]

   616 |             sprintf(tmp, "%s:%s", section, key);
       |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I reviewed especially the API changes. Most of them are cleanups only but two things should be pointed out:

- The type of the size field of struct _dictionary_ changed from int to ssize_t. The only user of this struct is lib/analogy/calibration.c which uses this structure for internal things only. It is never exposed to any public API so updating is OK and fully backward compatible.

- dictionary_new changed its signature
from dictionary_new(int size)
to dictionary_new(size_t size).
This function is not part of any public API. So updating does not break backward compatibility.

Quoted 8 lines
---
 lib/boilerplate/iniparser/dictionary.c | 409 ++++++++++----------
 lib/boilerplate/iniparser/dictionary.h |  43 ++-
 lib/boilerplate/iniparser/iniparser.c  | 491 +++++++++++++++++--------
 lib/boilerplate/iniparser/iniparser.h  | 131 +++++--
 4 files changed, 646 insertions(+), 428 deletions(-)

Thanks, applied to next (after sorting out our Siemens-local mail transfer problem...).

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux