Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-21 Thread Andreas Schwab
On Sep 21 2017, Junio C Hamano wrote: > Now you make me curious. How would that variant be different from > option C. in Jonathan's message? Only in the parity of the condition. Andreas. -- Andreas Schwab, sch...@linux-m68k.org GPG Key fingerprint = 58CA 54C7 6D53 942B

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-20 Thread Junio C Hamano
Andreas Schwab writes: > On Sep 20 2017, Jonathan Nieder wrote: > >> Andreas Schwab wrote: >>> On Sep 19 2017, Jonathan Nieder wrote: >> B. #define for_each_string_list_item(item, list) \ if (list->items) \

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-20 Thread Andreas Schwab
On Sep 20 2017, Jonathan Nieder wrote: > Andreas Schwab wrote: >> On Sep 19 2017, Jonathan Nieder wrote: > >>> B. #define for_each_string_list_item(item, list) \ >>> if (list->items) \ >>> for (item = ...; ...; ... ) >>> >>>This breaks

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-20 Thread Jonathan Nieder
Andreas Schwab wrote: > On Sep 19 2017, Jonathan Nieder wrote: >> B. #define for_each_string_list_item(item, list) \ >> if (list->items) \ >> for (item = ...; ...; ... ) >> >>This breaks a caller like >> if (foo) >>

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-20 Thread Andreas Schwab
On Sep 19 2017, Jonathan Nieder wrote: > B. #define for_each_string_list_item(item, list) \ > if (list->items) \ > for (item = ...; ...; ... ) > >This breaks a caller like > if (foo) > for_each_string_list_item(item, list) >

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-20 Thread Kaartic Sivaraam
On Wednesday 20 September 2017 10:57 AM, Jonathan Nieder wrote: Guard the loop with a NULL check to make the intent crystal clear to even the most pedantic compiler. A suitably clever compiler could let the NULL check only run in the first iteration, Noted this just now. So, the overhead

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-20 Thread Michael Haggerty
On 09/20/2017 07:27 AM, Jonathan Nieder wrote: > From: Michael Haggerty > > If you pass a newly initialized or newly cleared `string_list` to > `for_each_string_list_item()`, then the latter does > > for ( > item = (list)->items; /* NULL */ >

Re: [PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-19 Thread Junio C Hamano
Jonathan Nieder writes: > D. Eliminate for_each_string_list_item and let callers just do > > unsigned int i; > for (i = 0; i < list->nr; i++) { > struct string_list_item *item = list->items[i]; > ... > } > >Having to declare

[PATCH v2] for_each_string_list_item: avoid undefined behavior for empty list

2017-09-19 Thread Jonathan Nieder
From: Michael Haggerty If you pass a newly initialized or newly cleared `string_list` to `for_each_string_list_item()`, then the latter does for ( item = (list)->items; /* NULL */ item < (list)->items + (list)->nr; /* NULL + 0 */