git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] string-list.h: Add a value to string_list initializer lists
@ 2014-06-02  8:58 Tanay Abhra
  2014-06-02 10:54 ` Matthieu Moy
  2014-06-02 20:00 ` Junio C Hamano
  0 siblings, 2 replies; 3+ messages in thread
From: Tanay Abhra @ 2014-06-02  8:58 UTC (permalink / raw)
  To: git; +Cc: Tanay Abhra, Ramkumar Ramachandra, Matthieu Moy

Add a NULL value in the end of STRING_LIST_INIT_NODUP and
STRING_LIST_DUP to initialize `compare_strings_fn`.

Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
When I used a malloced string_list to play around with string-list API and
used the default init_list, it caused a seg fault. After an hour of debugging
I saw that comapre_strings_fn should be initialized to NULL. In C, even an
incomplete initialzer initializes every value to NULl or 0, so in normal
usage in the codebase this problem never occurs. Still it is better to be
thorough.

 string-list.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/string-list.h b/string-list.h
index de6769c..87ee419 100644
--- a/string-list.h
+++ b/string-list.h
@@ -15,8 +15,8 @@ struct string_list {
 	compare_strings_fn cmp; /* NULL uses strcmp() */
 };
 
-#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
-#define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1 }
+#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
+#define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1, NULL }
 
 void print_string_list(const struct string_list *p, const char *text);
 void string_list_clear(struct string_list *list, int free_util);
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] string-list.h: Add a value to string_list initializer lists
  2014-06-02  8:58 [PATCH] string-list.h: Add a value to string_list initializer lists Tanay Abhra
@ 2014-06-02 10:54 ` Matthieu Moy
  2014-06-02 20:00 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Matthieu Moy @ 2014-06-02 10:54 UTC (permalink / raw)
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> Add a NULL value in the end of STRING_LIST_INIT_NODUP and
> STRING_LIST_DUP to initialize `compare_strings_fn`.
>
> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
> ---
> When I used a malloced string_list to play around with string-list API and
> used the default init_list, it caused a seg fault. After an hour of debugging
> I saw that comapre_strings_fn should be initialized to NULL. In C, even an
> incomplete initialzer initializes every value to NULl or 0, so in normal
> usage in the codebase this problem never occurs. Still it is better to be
> thorough.

Part of this comment should be in the commit message itself. The "I
used ..." part should not (it is your experience), but the last two
sentences make sense IMHO.

Other than that, the changes sounds good.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] string-list.h: Add a value to string_list initializer lists
  2014-06-02  8:58 [PATCH] string-list.h: Add a value to string_list initializer lists Tanay Abhra
  2014-06-02 10:54 ` Matthieu Moy
@ 2014-06-02 20:00 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2014-06-02 20:00 UTC (permalink / raw)
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra, Matthieu Moy

Tanay Abhra <tanayabh@gmail.com> writes:

> Add a NULL value in the end of STRING_LIST_INIT_NODUP and
> STRING_LIST_DUP to initialize `compare_strings_fn`.

Hmph.  That is "what change is proposed", which we can read from the
patch text itself below.  We would want to see "why is this change a
good thing" to justify it.

As you mentioned later "In C, ...", there is nothing wrong in what
we have (and we can not quite read what exactly you tried to do with
uninitialized memory with these macros), so we need an excuse other
than correctness to justify this change.  Perhaps like this?

	STRING_LIST_INIT_{NODUP,DUP} initializers list values only
	for earlier structure members, relying on the usual
	convention in C that the omitted members are initailized to
	0, i.e. the former is expanded to the latter:

	    struct string_list l = STRING_LIST_INIT_DUP;
            struct string_list l = { NULL, 0, 0, 1 };

	and the last member that is not mentioned (i.e. 'cmp') is
	initialized to NULL.

	While there is nothing wrong in this construct, spelling out
	all the values where the macros are defined will serve also
	as a documentation, so let's do so.

> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
> ---
> When I used a malloced string_list to play around with string-list API and
> used the default init_list, it caused a seg fault. After an hour of debugging
> I saw that comapre_strings_fn should be initialized to NULL. In C, even an
> incomplete initialzer initializes every value to NULl or 0, so in normal
> usage in the codebase this problem never occurs. Still it is better to be
> thorough.
>
>  string-list.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/string-list.h b/string-list.h
> index de6769c..87ee419 100644
> --- a/string-list.h
> +++ b/string-list.h
> @@ -15,8 +15,8 @@ struct string_list {
>  	compare_strings_fn cmp; /* NULL uses strcmp() */
>  };
>  
> -#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
> -#define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1 }
> +#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL }
> +#define STRING_LIST_INIT_DUP   { NULL, 0, 0, 1, NULL }
>  
>  void print_string_list(const struct string_list *p, const char *text);
>  void string_list_clear(struct string_list *list, int free_util);

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-06-02 20:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-02  8:58 [PATCH] string-list.h: Add a value to string_list initializer lists Tanay Abhra
2014-06-02 10:54 ` Matthieu Moy
2014-06-02 20:00 ` Junio C Hamano

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