[ccan] [PATCH] stringbuilder: Functions for joining strings.

David Gibson david at gibson.dropbear.id.au
Fri Sep 5 14:52:13 EST 2014


On Sat, Aug 23, 2014 at 09:25:09AM +1000, stuartl at longlandclan.yi.org wrote:
> From: Stuart Longland <stuartl at longlandclan.yi.org>
> 
> This is a small couple of functions for joining lists of strings
> together.  The lists can either be varadic arguments or arrays, and
> delimiters are optional.
> 
> This patch incorporates some advice from David Gibson on the original
> module.


[snip]
> +int main(int argc, char *argv[])
> +{
> +	if (argc != 2)
> +		return 1;
> +
> +	if (strcmp(argv[1], "depends") == 0) {
> +		/*
> +		 * This triggers a circular dependency!
> +		 * printf("ccan/str\n");

We really need to understand why, because it's not obvious.

But.. this should probably go in "testdepends" anyway, since it's only
used by the testcases, not the core of the module.

[snip]
> --- /dev/null
> +++ b/ccan/stringbuilder/stringbuilder.c
> @@ -0,0 +1,77 @@
> +/* CC0 (Public domain) - see LICENSE file for details */
> +#include <ccan/stringbuilder/stringbuilder.h>
> +#include <string.h>
> +#include <errno.h>
> +
> +int stringbuilder_args(char* str, size_t str_sz, const char* delim, ...)
> +{
> +	int res;
> +	va_list ap;
> +	va_start(ap, delim);
> +	res = stringbuilder_va(str, str_sz, delim, ap);
> +	va_end(ap);
> +	return res;
> +}
> +
> +static int stringbuilder_cpy(
> +		char** str, size_t* str_sz, const char* s, size_t s_len)
> +{
> +	if (!s)
> +		return 0;
> +
> +	if (*str != s) {
> +		if (!s_len)
> +			s_len = strlen(s);
> +		if (s_len > *str_sz)
> +			return EMSGSIZE;

I thought you were going to switch this out for setting errno and
using negative error codes.

> +		strcpy(*str, s);

So, if this is called badly, with s_len != strlen(s), this could have
unexpected effects.  Using memcpy(*str, s, s_len) swould be safer.
-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/ccan/attachments/20140905/364d043d/attachment.sig>


More information about the ccan mailing list