ccan.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Damien Grassart <damien@grassart.com>
Cc: ccan@lists.ozlabs.org
Subject: Re: [PATCH 2/2] darray: Fix bug in the darray_remove() macro
Date: Mon, 28 Aug 2017 12:48:41 +1000	[thread overview]
Message-ID: <20170828024841.GC2578@umbus.fritz.box> (raw)
In-Reply-To: <20170827212624.14673-2-damien@grassart.com>


[-- Attachment #1.1: Type: text/plain, Size: 3501 bytes --]

On Sun, Aug 27, 2017 at 11:26:24PM +0200, Damien Grassart wrote:
> The memmove() call should be using the index argument to determine the
> number of bytes to copy. To be consistent with the rest of the code,
> we should also not evaluate the index parameter multiple
> times. Calling this with rand() % arr.size would otherwise generally
> segfault.
> 
> Finally, we want to avoid using "index" as an identifier so as to not
> shadow index(3) in the C library.
> 
> Signed-off-by: Damien Grassart <damien@grassart.com>

This breaks compile for me, though I can't quickly see why.

$ make
cc -g3 -ggdb -Wall -Wstrict-prototypes -Wold-style-definition -Wundef -Wmissing-prototypes -Wmissing-declarations -Wpointer-arith -Wwrite-strings -DCCAN_STR_DEBUG=1 -I.  -MMD -MP -MFccan/strgrp/strgrp.o.d -MTccan/strgrp/strgrp.o -c ccan/strgrp/strgrp.c -o ccan/strgrp/strgrp.o
In file included from ccan/strgrp/strgrp.c:26:0:
./ccan/darray/darray.h:235:2: error: expected identifier or ‘(’ before ‘if’
  if (index_ < arr.size-1)    \
  ^~
./ccan/darray/darray.h:237:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
  (arr).size--;  \
       ^
./ccan/darray/darray.h:238:2: error: expected identifier or ‘(’ before ‘}’ token
  } while(0)
  ^
./ccan/darray/darray.h:238:4: error: expected identifier or ‘(’ before ‘while’
  } while(0)
    ^~~~~

I also noticed that both this and the earlier patches use:
	size_t index_ = i;
in the macro, without parentheses around the 'i' macro paramater.
That's not the cause of the error above, but it's not good practice as
a rule.

So, I've backed out these darray patches for now.  Can you debug the
compile problem above and resend the whole lot as a single series.

> ---
>  ccan/darray/darray.h | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/ccan/darray/darray.h b/ccan/darray/darray.h
> index 82726c05..6787f14c 100644
> --- a/ccan/darray/darray.h
> +++ b/ccan/darray/darray.h
> @@ -170,8 +170,8 @@ typedef darray(unsigned long)  darray_ulong;
>  		memmove((arr).item+1, (arr).item, ((arr).size-1)*sizeof(*(arr).item)); \
>  		(arr).item[0] = (__VA_ARGS__); \
>  	} while(0)
> -#define darray_insert(arr, index, ...) do { \
> -		size_t index_ = index; \
> +#define darray_insert(arr, i, ...) do { \
> +		size_t index_ = i; \
>  		darray_resize(arr, (arr).size+1); \
>  		memmove((arr).item+index_+1, (arr).item+index_, ((arr).size-index_-1)*sizeof(*(arr).item)); \
>  		(arr).item[index_] = (__VA_ARGS__); \
> @@ -230,9 +230,10 @@ typedef darray(unsigned long)  darray_ulong;
>  #define darray_pop(arr) ((arr).item[--(arr).size])
>  #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
>  /* Warning, slow: Requires copying all elements after removed item. */
> -#define darray_remove(arr, index) do { \
> -	if (index < arr.size-1)    \
> -		memmove(&(arr).item[index], &(arr).item[index+1], ((arr).size-1-i)*sizeof(*(arr).item)); \
> +#define darray_remove(arr, i) do { \
> +	size_t index_ = i;
> +	if (index_ < arr.size-1)    \
> +		memmove(&(arr).item[index_], &(arr).item[index_+1], ((arr).size-1-index_)*sizeof(*(arr).item)); \
>  	(arr).size--;  \
>  	} while(0)
>  

-- 
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

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 127 bytes --]

_______________________________________________
ccan mailing list
ccan@lists.ozlabs.org
https://lists.ozlabs.org/listinfo/ccan

  reply	other threads:[~2017-08-28  3:57 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-26 16:26 [PATCH 1/2] darray: Fix bug in the darray_remove() macro Damien Grassart
2017-08-27  2:56 ` David Gibson
2017-08-27 21:02   ` Damien Grassart
2017-08-27 21:26   ` [PATCH 1/2] darray: Rename identifiers starting with an underscore Damien Grassart
2017-08-27 21:26     ` [PATCH 2/2] darray: Fix bug in the darray_remove() macro Damien Grassart
2017-08-28  2:48       ` David Gibson [this message]
2017-08-28  5:03         ` Damien Grassart
2017-08-28  5:09         ` [PATCH] " Damien Grassart
2017-08-29  4:54           ` David Gibson
2017-08-29 10:08             ` Damien Grassart
2017-08-28  2:43     ` [PATCH 1/2] darray: Rename identifiers starting with an underscore David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170828024841.GC2578@umbus.fritz.box \
    --to=david@gibson.dropbear.id.au \
    --cc=ccan@lists.ozlabs.org \
    --cc=damien@grassart.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).