git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: shejialuo <shejialuo@gmail.com>
Cc: git@vger.kernel.org,  Patrick Steinhardt <ps@pks.im>
Subject: Re: [PATCH 1/5] string-list: fix sign compare warnings
Date: Tue, 22 Apr 2025 14:02:18 -0700	[thread overview]
Message-ID: <xmqqy0vr3o6d.fsf@gitster.g> (raw)
In-Reply-To: <aAett8cJuDJ_FSdw@ArchLinux> (shejialuo@gmail.com's message of "Tue, 22 Apr 2025 22:54:47 +0800")

shejialuo <shejialuo@gmail.com> writes:

> However, for "string-list.c::add_entry" function, we compare the `index`
> of the `int` type with the `list->nr` of unsigned type. It seems that
> we could just simply convert the type of `index` from `int` to
> `size_t`. But actually this is a correct behavior.

Sorry, but I am lost by the last sentence.

"this" that is a correct behavior refers to...?  That the incoming
parameter insert_at and the local variable index are both of signed
integer?

> We would set the `index` value by checking whether `insert_at` is -1.
> If not, we would set `index` to be `insert_at`, otherwise we would use
> "get_entry_index` to find the inserted position.

To rephrase the above (simply because the above is literal English
translation from what C says), the caller either can pass -1 to mean
"find an appropriate location in the list to keep it sorted", or an
index into the list->items[] array to specify exactly where the item
should be inserted.

Naturally, insert_at must be either -1 (auto), or between 0
(i.e. the candidate is smaller than anything in the list) and
list->nr (i.e. the candidate is larger than everything in the list)
inclusive.  Any other value is invalid.  I think that is a more
appropriate thing to say than ...

> What if the caller passes a negative value except "-1", the compiler
> would convert the `index` to be a positive value which would make the
> `if` statement be false to avoid moving array. However, we would
> definitely encounter trouble when setting the inserted item.

... this paragraph.  Not moving is _not_ avoiding problem, so it is
immaterial.  The lack of valid range check before using the index
is.

> And we only call "add_entry" in "string_list_insert" function, and we
> simply pass "-1" for "insert_at" parameter. So, we never use this
> parameter to insert element in a user specified position. Let's delete
> this parameter. If there is any requirement later, we may use a better
> way to do this. And then we could safely convert the index to be
> `size_t` when comparing.

Good.  As we only use the "auto" setting with this code now, as long
as get_entry_index() returns a value between 0 and list->nr, the
lack of such range checking in the original code no longer is an
issue.

Having said that, in the longer run, get_entry_index() would want to
return size_t simply because it is returning a value between 0 and
list->nr, whose type is size_t.   left/mid/right variables also need
to become size_t and the loop initialization may have to be tweaked
(since the current code strangely starts left with -1 which would
never be the index into the array), but fixing that should probably
make the loop easier to read, which is a bonus.

And add_entry(), since it needs to do the usual -1-pos dance to
indicate where things would have been returned, would return
ssize_t---or better yet, it can just turned into returning size_t
with an extra out parameter (just like the exact_match out parameter
get_entry_index() has) to indicate if we already had the same item
in the list already.  It is perfectly fine to leave it outside the
scope of this series, but if you are tweaking all the callers of
add_entry() anyway in this step, you may want to bite the bullet and
just go all the way.

Thanks.

  reply	other threads:[~2025-04-22 21:02 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22 14:53 [PATCH 0/5] enhance "string_list" code and test shejialuo
2025-04-22 14:54 ` [PATCH 1/5] string-list: fix sign compare warnings shejialuo
2025-04-22 21:02   ` Junio C Hamano [this message]
2025-04-24 12:27     ` shejialuo
2025-04-22 14:54 ` [PATCH 2/5] u-string-list: move "test_split" into "u-string-list.c" shejialuo
2025-04-22 21:27   ` Junio C Hamano
2025-04-24 12:50     ` shejialuo
2025-04-23 11:52   ` Patrick Steinhardt
2025-04-24 12:53     ` shejialuo
2025-04-22 14:55 ` [PATCH 3/5] u-string-list: move "test_split_in_place" to "u-string-list.c" shejialuo
2025-04-23 11:53   ` Patrick Steinhardt
2025-04-22 14:55 ` [PATCH 4/5] u-string-list: move "filter string" test " shejialuo
2025-04-22 14:55 ` [PATCH 5/5] u-string-list: move "remove duplicates" " shejialuo
2025-04-23 11:53   ` Patrick Steinhardt
2025-04-24 12:56     ` shejialuo
2025-05-18 15:55 ` [PATCH v2 0/8] enhance "string_list" code and test shejialuo
2025-05-18 15:56   ` [PATCH v2 1/8] string-list: fix sign compare warnings for loop iterator shejialuo
2025-05-19  7:17     ` Patrick Steinhardt
2025-05-26 13:50       ` shejialuo
2025-05-18 15:57   ` [PATCH v2 2/8] string-list: remove unused "insert_at" parameter from add_entry shejialuo
2025-05-19  7:17     ` Patrick Steinhardt
2025-05-26 13:52       ` shejialuo
2025-05-19  7:51     ` Jeff King
2025-05-26 14:01       ` shejialuo
2025-05-26 14:21         ` Patrick Steinhardt
2025-05-18 15:57   ` [PATCH v2 3/8] string-list: return index directly when inserting an existing element shejialuo
2025-05-19  7:18     ` Patrick Steinhardt
2025-05-26 14:13       ` shejialuo
2025-05-19  7:58     ` Jeff King
2025-05-26 14:20       ` shejialuo
2025-05-18 15:57   ` [PATCH v2 4/8] string-list: enable sign compare warnings check shejialuo
2025-05-19  7:18     ` Patrick Steinhardt
2025-05-26 14:27       ` shejialuo
2025-05-18 15:57   ` [PATCH v2 5/8] u-string-list: move "test_split" into "u-string-list.c" shejialuo
2025-05-19  7:17     ` Patrick Steinhardt
2025-05-18 15:57   ` [PATCH v2 6/8] u-string-list: move "test_split_in_place" to "u-string-list.c" shejialuo
2025-05-18 15:58   ` [PATCH v2 7/8] u-string-list: move "filter string" test " shejialuo
2025-05-19  7:18     ` Patrick Steinhardt
2025-05-26 14:33       ` shejialuo
2025-05-18 15:58   ` [PATCH v2 8/8] u-string-list: move "remove duplicates" " shejialuo
2025-05-19  7:18     ` Patrick Steinhardt
2025-06-29  4:26   ` [PATCH v3 0/8] enhance "string_list" code and test shejialuo
2025-06-29  4:27     ` [PATCH v3 1/8] string-list: fix sign compare warnings for loop iterator shejialuo
2025-06-29  4:27     ` [PATCH v3 2/8] string-list: remove unused "insert_at" parameter from add_entry shejialuo
2025-06-29  4:27     ` [PATCH v3 3/8] string-list: return index directly when inserting an existing element shejialuo
2025-06-29  4:28     ` [PATCH v3 4/8] string-list: enable sign compare warnings check shejialuo
2025-06-29  4:28     ` [PATCH v3 5/8] u-string-list: move "test_split" into "u-string-list.c" shejialuo
2025-06-29  4:28     ` [PATCH v3 6/8] u-string-list: move "test_split_in_place" to "u-string-list.c" shejialuo
2025-06-29  4:28     ` [PATCH v3 7/8] u-string-list: move "filter string" test " shejialuo
2025-06-29  4:28     ` [PATCH v3 8/8] u-string-list: move "remove duplicates" " shejialuo
2025-07-04  5:24     ` [PATCH v3 0/8] enhance "string_list" code and test Patrick Steinhardt
2025-07-07 15:10       ` Junio C Hamano

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=xmqqy0vr3o6d.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    --cc=shejialuo@gmail.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).