[Mesa-dev] [PATCH 1/8] util: Add a string buffer implementation

Nicolai Hähnle nhaehnle at gmail.com
Wed Sep 6 08:41:41 UTC 2017


On 29.08.2017 21:56, Thomas Helland wrote:
> Based on Vladislav Egorovs work on the preprocessor, but split
> out to a util functionality that should be universal. Setup, teardown,
> memory handling and general layout is modeled around the hash_table
> and the set, to make it familiar for everyone.
> 
> A notable change is that this implementation is always null terminated.
> The rationale is that it will be less error-prone, as one might
> access the buffer directly, thereby reading a non-terminated string.
> Also, vsnprintf and friends prints the null-terminator.
> 
> V2: Address review feedback from Timothy and Grazvydas
>     - Fix MINGW preprocessor check
>     - Changed len from uint to int
>     - Make string argument const in append function
>     - Move to header and inline append function
>     - Add crimp_to_fit function for resizing buffer
> 
> V3: Move include of ralloc to string_buffer.h
> 
> V4: Use u_string.h for a cross-platform working vsnprintf
> ---
>   src/util/Makefile.sources |   2 +
>   src/util/string_buffer.c  | 155 ++++++++++++++++++++++++++++++++++++++++++++++
>   src/util/string_buffer.h  |  87 ++++++++++++++++++++++++++
>   3 files changed, 244 insertions(+)
>   create mode 100644 src/util/string_buffer.c
>   create mode 100644 src/util/string_buffer.h
> 
> diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources
> index 4ed4e39f03..c7f6516a99 100644
> --- a/src/util/Makefile.sources
> +++ b/src/util/Makefile.sources
> @@ -37,6 +37,8 @@ MESA_UTIL_FILES := \
>   	simple_list.h \
>   	slab.c \
>   	slab.h \
> +	string_buffer.c \
> +	string_buffer.h \
>   	strndup.h \
>   	strtod.c \
>   	strtod.h \
> diff --git a/src/util/string_buffer.c b/src/util/string_buffer.c
> new file mode 100644
> index 0000000000..e2fa612976
> --- /dev/null
> +++ b/src/util/string_buffer.c
> @@ -0,0 +1,155 @@
> +/*
> + * Copyright © 2017 Thomas Helland
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + *
> + */
> +#include "string_buffer.h"
> +
> +static bool
> +ensure_capacity(struct _mesa_string_buffer *str, uint32_t needed_capacity)
> +{
> +   if (needed_capacity <= str->capacity)
> +      return true;
> +
> +   /* Too small, we have to resize our buffer.
> +    * Double until we can fit the new string.
> +    */
> +   uint32_t new_capacity = str->capacity * 2;
> +   while (needed_capacity > new_capacity)
> +      new_capacity *= 2;
> +
> +   str->buf = reralloc_array_size(str, str->buf, sizeof(char), new_capacity);
> +   if (str->buf == NULL)
> +      return false;
> +
> +   str->capacity = new_capacity;
> +   return true;
> +}
> +
> +struct _mesa_string_buffer *
> +_mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity)
> +{
> +   struct _mesa_string_buffer *str;
> +   str = ralloc(mem_ctx, struct _mesa_string_buffer);
> +
> +   if (str == NULL)
> +      return NULL;
> +
> +   /* If no initial capacity is set then set it to something */
> +   str->capacity = initial_capacity ? initial_capacity : 8;
> +   str->buf = ralloc_array(str, char, str->capacity);

Needs to handle !str->buf.


> +   str->length = 0;
> +   str->buf[str->length] = '\0';
> +   return str;
> +}
> +
> +void
> +_mesa_string_buffer_destroy(struct _mesa_string_buffer *str)
> +{
> +   ralloc_free(str);
> +}
> +
> +bool
> +_mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
> +                               uint32_t num_args, ...)
> +{
> +   int i;
> +   char* s;
> +   va_list args;
> +   va_start(args, num_args);
> +   for (i = 0; i < num_args; i++) {
> +      s = va_arg(args, char*);
> +      if (!_mesa_string_buffer_append_len(str, s, strlen(s)))
> +         return false;

You need to call va_end(args) here (or do some goto out; thing).


> +   }
> +   va_end(args);
> +   return true;
> +}
> +
> +bool
> +_mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
> +                               const char *c, uint32_t len)
> +{
> +   uint32_t needed_length = str->length + len + 1;

Please add an overflow check. Since you're using unsigned arithmetic:

     if (needed_length < str->length)
         return false;


> +   if (!ensure_capacity(str, needed_length))
> +      return false;
> +
> +   memcpy(str->buf + str->length, c, len);
> +   str->length += len;
> +   str->buf[str->length] = '\0';
> +   return true;
> +}
> +
> +void
> +_mesa_string_buffer_clear(struct _mesa_string_buffer *str)
> +{
> +   str->length = 0;
> +   str->buf[str->length] = '\0';
> +}
> +
> +uint32_t
> +_mesa_string_buffer_space_left(struct _mesa_string_buffer *str)

This function seems like an odd implementation detail to expose. I'd 
leave it out of the public interface.


> +{
> +   return str->capacity - str->length - 1;
> +}
> +
> +bool
> +_mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
> +                            const char *format, va_list args)
> +{
> +   int32_t len;
> +   uint32_t end = str->length;
> +   uint32_t size = MAX2(100, _mesa_string_buffer_space_left(str));
> +
> +   while (1) {
> +      ensure_capacity(str, end + size + 1);
> +      len = util_vsnprintf(str->buf + end, size, format, args);
> +
> +      /* Fail if an error happened in vsnprintf()
> +       * or if measured len overflows size_t
> +       */
> +      if (unlikely(len < 0 || str->length + len + 1 < str->length))
> +         return false;
> +
> +      /* There was enough room for the string, so we're done */
> +      if (len < size) {
> +         str->length += len;
> +         return true;
> +      }
> +
> +      /* Formatted string is too large for the buffer.
> +       * Increase the size and print again.
> +       */
> +      size = MAX2(size * 2, str->length + len);
> +   }

Using a loop here is not necessary. Two calls to util_vsnprintf should 
always be sufficient, since that function always returns the length of 
the fully formatted string (i.e. in case of overflow, it returns the 
length of the string (excluding NUL) that would have been written had 
there been enough space).

Also, the /size/ parameter to util_vsnprintf should always be just 
str->capacity - str->length.

Cheers,
Nicolai


> +}
> +
> +bool
> +_mesa_string_buffer_printf(struct _mesa_string_buffer *str,
> +                            const char *format, ...)
> +{
> +   bool res;
> +   va_list args;
> +   va_start(args, format);
> +   res = _mesa_string_buffer_vprintf(str, format, args);
> +   va_end(args);
> +   return res;
> +}
> diff --git a/src/util/string_buffer.h b/src/util/string_buffer.h
> new file mode 100644
> index 0000000000..e896dbeee5
> --- /dev/null
> +++ b/src/util/string_buffer.h
> @@ -0,0 +1,87 @@
> +/*
> + * Copyright © 2017 Thomas Helland
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
> + * IN THE SOFTWARE.
> + *
> + */
> +#ifndef _STRING_BUFFER_H
> +#define _STRING_BUFFER_H
> +
> +#include "ralloc.h"
> +#include "u_string.h"
> +#include <stdbool.h>
> +#include <stdint.h>
> +#include <stdio.h>
> +#include <string.h>
> +
> +struct _mesa_string_buffer {
> +   char *buf;
> +   uint32_t length;
> +   uint32_t capacity;
> +};
> +
> +struct _mesa_string_buffer *
> +_mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity);
> +
> +static inline void
> +_mesa_string_buffer_crimp_to_fit(struct _mesa_string_buffer *str)
> +{
> +   str->capacity = str->length + 1;
> +   str->buf = reralloc_array_size(str, str->buf, sizeof(char), str->capacity);
> +}
> +
> +void
> +_mesa_string_buffer_destroy(struct _mesa_string_buffer *str);
> +
> +bool
> +_mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
> +                               uint32_t num_args, ...);
> +bool
> +_mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
> +                               const char *c, uint32_t len);
> +
> +static inline bool
> +_mesa_string_buffer_append_char(struct _mesa_string_buffer *str, char c)
> +{
> +   assert(str->capacity > 0 && c != '\0');
> +   return _mesa_string_buffer_append_len(str, &c, 1);
> +}
> +
> +static inline bool
> +_mesa_string_buffer_append(struct _mesa_string_buffer *str, const char *c)
> +{
> +   return _mesa_string_buffer_append_len(str, c, strlen(c));
> +}
> +
> +void
> +_mesa_string_buffer_clear(struct _mesa_string_buffer *str);
> +
> +uint32_t
> +_mesa_string_buffer_space_left(struct _mesa_string_buffer *str);
> +
> +bool
> +_mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
> +                            const char *format, va_list args);
> +
> +bool
> +_mesa_string_buffer_printf(struct _mesa_string_buffer *str,
> +                            const char *format, ...);
> +
> +#endif /* _STRING_BUFFER_H */
> 


-- 
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.


More information about the mesa-dev mailing list