git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership
@ 2022-04-27  8:04 Carlo Marcelo Arenas Belón
       [not found] ` <CA+zfrf-6c7BG-PDehHKh6_8zWdu=NeM9gL6zN8Ug+oT9fAOfqw@mail.gmail.com>
  2022-04-27 16:35 ` Junio C Hamano
  0 siblings, 2 replies; 5+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2022-04-27  8:04 UTC (permalink / raw)
  To: git
  Cc: me, derrickstolee, gitster, Johannes.Schindelin,
	Carlo Marcelo Arenas Belón

8959555cee7 (setup_git_directory(): add an owner check for the top-level
directory, 2022-03-02) adds this member as part of a newly created
structure that then gets initialized during the callback, but bb50ec3cc30
(setup: fix safe.directory key not being checked, 2022-04-13) add a
quick exit from the callback that avoids this initialization unless the
callback is called with the relevant key.

This leads to this variable not being initialized UNLESS the global config
has at least one key for safe.directory, so instead initialize it in the
caller.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setup.c b/setup.c
index a7b36f3ffbf..17c7f5fc1dc 100644
--- a/setup.c
+++ b/setup.c
@@ -1122,7 +1122,7 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
 
 static int ensure_valid_ownership(const char *path)
 {
-	struct safe_directory_data data = { .path = path };
+	struct safe_directory_data data = { .path = path, .is_safe = 0 };
 
 	if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
 	    is_path_owned_by_current_user(path))
-- 
2.36.0.266.g59f845bde02


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

* Re: [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership
       [not found] ` <CA+zfrf-6c7BG-PDehHKh6_8zWdu=NeM9gL6zN8Ug+oT9fAOfqw@mail.gmail.com>
@ 2022-04-27  8:31   ` Carlo Arenas
       [not found]     ` <CA+zfrf_mHfDXk-1VhU564YeCTW7rgDeeuORdTT61LkTESmC5Og@mail.gmail.com>
  0 siblings, 1 reply; 5+ messages in thread
From: Carlo Arenas @ 2022-04-27  8:31 UTC (permalink / raw)
  To: Matheus Valadares; +Cc: Johannes.Schindelin, derrickstolee, git, gitster

On Wed, Apr 27, 2022 at 1:17 AM Matheus Valadares <me@m28.io> wrote:
>
> That’s not needed. Fields in an aggregate constructor that are not named are initialized to 0.

Could you point me to the C99 specification that documents that if you
have it at hand?  Even if not strictly necessary, it seems like good
code higiene as its effect is hidden.

FWIW, it broke a change I was doing on top of this where I removed the
" .path " initializer to set it later and was suddenly getting random
unrelated errors because this field was not initialized.

Carlo

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

* Re: [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership
       [not found]     ` <CA+zfrf_mHfDXk-1VhU564YeCTW7rgDeeuORdTT61LkTESmC5Og@mail.gmail.com>
@ 2022-04-27  9:16       ` Carlo Arenas
  2022-04-27 14:04         ` Derrick Stolee
  0 siblings, 1 reply; 5+ messages in thread
From: Carlo Arenas @ 2022-04-27  9:16 UTC (permalink / raw)
  To: Matheus Valadares; +Cc: Johannes.Schindelin, derrickstolee, git, gitster

On Wed, Apr 27, 2022 at 1:33 AM Matheus Valadares <me@m28.io> wrote:
>
> C99 standard §6.7.8 (Initialization)/21,
>
> If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

Thanks, I will include the change to remove the path initializer and
add instead the is_safe one as part of my bigger change, but FWIW the
following doesn't even trigger a warning with the highest level we
have with neither a recent clang or gcc or even the cppcheck static
analyzer, but leave and uses is_safe uninitialized.

diff --git a/setup.c b/setup.c
index 17c7f5fc1dc..28d008145fa 100644
--- a/setup.c
+++ b/setup.c
@@ -1122,7 +1122,9 @@ static int safe_directory_cb(const char *key,
const char *value, void *d)

 static int ensure_valid_ownership(const char *path)
 {
-       struct safe_directory_data data = { .path = path, .is_safe = 0 };
+       struct safe_directory_data data;
+
+       data.path = path;

        if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
            is_path_owned_by_current_user(path))

Carlo

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

* Re: [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership
  2022-04-27  9:16       ` Carlo Arenas
@ 2022-04-27 14:04         ` Derrick Stolee
  0 siblings, 0 replies; 5+ messages in thread
From: Derrick Stolee @ 2022-04-27 14:04 UTC (permalink / raw)
  To: Carlo Arenas, Matheus Valadares; +Cc: Johannes.Schindelin, git, gitster

On 4/27/2022 5:16 AM, Carlo Arenas wrote:
> On Wed, Apr 27, 2022 at 1:33 AM Matheus Valadares <me@m28.io> wrote:
>>
>> C99 standard §6.7.8 (Initialization)/21,
>>
>> If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
> 
> Thanks, I will include the change to remove the path initializer and
> add instead the is_safe one as part of my bigger change, but FWIW the
> following doesn't even trigger a warning with the highest level we
> have with neither a recent clang or gcc or even the cppcheck static
> analyzer, but leave and uses is_safe uninitialized.
> 
> diff --git a/setup.c b/setup.c
> index 17c7f5fc1dc..28d008145fa 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -1122,7 +1122,9 @@ static int safe_directory_cb(const char *key,
> const char *value, void *d)
> 
>  static int ensure_valid_ownership(const char *path)
>  {
> -       struct safe_directory_data data = { .path = path, .is_safe = 0 };

Here, we are using an initializer, which guarantees that the unmentioned
members are set to zeroes.

> +       struct safe_directory_data data;

Here, you are not using an initializer. The data could be anything, so
is not safe.

> +
> +       data.path = path;

Initializing individual members like this afterwards is not safe unless
you set all members individually.

That is why we use the "= { ... }" pattern throughout the codebase.
Sometimes it is simplified to just "= { 0 }" to make sure that the first
member is mentioned as zero and the remaining members are also set to
zero as specified in the standard.

Thanks,
-Stolee

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

* Re: [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership
  2022-04-27  8:04 [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership Carlo Marcelo Arenas Belón
       [not found] ` <CA+zfrf-6c7BG-PDehHKh6_8zWdu=NeM9gL6zN8Ug+oT9fAOfqw@mail.gmail.com>
@ 2022-04-27 16:35 ` Junio C Hamano
  1 sibling, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2022-04-27 16:35 UTC (permalink / raw)
  To: Carlo Marcelo Arenas Belón
  Cc: git, me, derrickstolee, Johannes.Schindelin

Carlo Marcelo Arenas Belón  <carenas@gmail.com> writes:

> 8959555cee7 (setup_git_directory(): add an owner check for the top-level
> directory, 2022-03-02) adds this member as part of a newly created
> structure that then gets initialized during the callback, but bb50ec3cc30
> (setup: fix safe.directory key not being checked, 2022-04-13) add a
> quick exit from the callback that avoids this initialization unless the
> callback is called with the relevant key.
>
> This leads to this variable not being initialized UNLESS the global config
> has at least one key for safe.directory, so instead initialize it in the
> caller.
>
> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
> ---
>  setup.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/setup.c b/setup.c
> index a7b36f3ffbf..17c7f5fc1dc 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -1122,7 +1122,7 @@ static int safe_directory_cb(const char *key, const char *value, void *d)
>  
>  static int ensure_valid_ownership(const char *path)
>  {
> -	struct safe_directory_data data = { .path = path };
> +	struct safe_directory_data data = { .path = path, .is_safe = 0 };

This is not wrong per-se but is not necessary.  Once you have an
initializer, the struct is zero initialized except for members whose
initial values are explicitly mentioned in the initializer.

Sometimes an explicit initialization is a good way to make the
intention of the code clear, and because setting of the .is_safe
member is done inside a callback function, out of sight from the
reader of this function, while the return value does depend on
having a valid value in the .is_safe member, I do not think we mind
this change, though.

But if we were to take it, the justification must be rewritten.  It
is an OK change to clarify the code to human readers.  It is not a
fix to a bug that left a struct member uninitialized.

>  
>  	if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
>  	    is_path_owned_by_current_user(path))

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

end of thread, other threads:[~2022-04-27 16:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-27  8:04 [PATCH] setup: avoid uninitialized use of is_safe in ensure_valid_ownership Carlo Marcelo Arenas Belón
     [not found] ` <CA+zfrf-6c7BG-PDehHKh6_8zWdu=NeM9gL6zN8Ug+oT9fAOfqw@mail.gmail.com>
2022-04-27  8:31   ` Carlo Arenas
     [not found]     ` <CA+zfrf_mHfDXk-1VhU564YeCTW7rgDeeuORdTT61LkTESmC5Og@mail.gmail.com>
2022-04-27  9:16       ` Carlo Arenas
2022-04-27 14:04         ` Derrick Stolee
2022-04-27 16:35 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).