* [PATCH] sideband.c: replace int with size_t for clarity
@ 2023-12-22 17:01 Chandra Pratap via GitGitGadget
2023-12-22 17:57 ` Torsten Bögershausen
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Chandra Pratap via GitGitGadget @ 2023-12-22 17:01 UTC (permalink / raw)
To: git; +Cc: Chandra Pratap, Chandra Pratap
From: Chandra Pratap <chandrapratap3519@gmail.com>
Replace int with size_t for clarity and remove the
'NEEDSWORK' tag associated with it.
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
sideband.c: replace int with size_t for clarity
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1625
sideband.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/sideband.c b/sideband.c
index 6cbfd391c47..1599e408d1b 100644
--- a/sideband.c
+++ b/sideband.c
@@ -69,9 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
* of the line. This should be called for a single line only, which is
* passed as the first N characters of the SRC array.
*
- * NEEDSWORK: use "size_t n" instead for clarity.
*/
-static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
{
int i;
base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
--
gitgitgadget
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] sideband.c: replace int with size_t for clarity
2023-12-22 17:01 [PATCH] sideband.c: replace int with size_t for clarity Chandra Pratap via GitGitGadget
@ 2023-12-22 17:57 ` Torsten Bögershausen
2023-12-22 18:27 ` Chandra Pratap
2023-12-22 19:01 ` Junio C Hamano
2023-12-23 17:03 ` [PATCH v2] " Chandra Pratap via GitGitGadget
2023-12-26 17:14 ` [PATCH] sideband.c: replace int with size_t for clarity Junio C Hamano
2 siblings, 2 replies; 12+ messages in thread
From: Torsten Bögershausen @ 2023-12-22 17:57 UTC (permalink / raw)
To: Chandra Pratap via GitGitGadget; +Cc: git, Chandra Pratap, Chandra Pratap
On Fri, Dec 22, 2023 at 05:01:09PM +0000, Chandra Pratap via GitGitGadget wrote:
> From: Chandra Pratap <chandrapratap3519@gmail.com>
>
> Replace int with size_t for clarity and remove the
> 'NEEDSWORK' tag associated with it.
>
> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
> ---
> sideband.c: replace int with size_t for clarity
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/1625
>
> sideband.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/sideband.c b/sideband.c
> index 6cbfd391c47..1599e408d1b 100644
> --- a/sideband.c
> +++ b/sideband.c
> @@ -69,9 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
> * of the line. This should be called for a single line only, which is
> * passed as the first N characters of the SRC array.
> *
> - * NEEDSWORK: use "size_t n" instead for clarity.
> */
> -static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> +static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
> {
> int i;
>
Thanks for working on this.
There is, however, more potential for improvements.
First of all: the "int i" from above.
Further down, we read
for (i = 0; i < ARRAY_SIZE(keywords); i++) {
However, a size of an array can never be negative, so that
an unsigned data type is a better choice than a signed.
And, arrays can have more elements than an int can address,
at least in theory.
For a reader it makes more sense, to replace
int i;
with
size_t i;
And further down, there is another place for improvments:
int len = strlen(p->keyword);
if (n < len)
continue;
Even here, a strlen is never negative. And a size_t is the choice for len,
since all "modern" implementations declare strlen() to return size_t
Do you think that you can have a look at these changes ?
I will be happy to do a review, and possibly other people as well.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] sideband.c: replace int with size_t for clarity
2023-12-22 17:57 ` Torsten Bögershausen
@ 2023-12-22 18:27 ` Chandra Pratap
2023-12-22 18:39 ` Torsten Bögershausen
2023-12-22 19:01 ` Junio C Hamano
1 sibling, 1 reply; 12+ messages in thread
From: Chandra Pratap @ 2023-12-22 18:27 UTC (permalink / raw)
To: Torsten Bögershausen
Cc: Chandra Pratap via GitGitGadget, git, Chandra Pratap
Thanks for the feedback, Torsten. I was working on improving the rest of
sideband.c as you suggested when I encountered this snippet on line 82:
while (0 < n && isspace(*src)) {
strbuf_addch(dest, *src);
src++;
n--;
}
Here, we are decreasing the value of an unsigned type to potentially below
0 which may lead to overflow and result in some nasty bug. In this case,
is it wise to continue with replacing 'int n' with 'size_t n' as the
NEEDSWORK tag suggests or should we improve upon the rest of the file
and revert 'size_t n' to 'int n' ?
On Fri, 22 Dec 2023 at 23:27, Torsten Bögershausen <tboegi@web.de> wrote:
>
> On Fri, Dec 22, 2023 at 05:01:09PM +0000, Chandra Pratap via GitGitGadget wrote:
> > From: Chandra Pratap <chandrapratap3519@gmail.com>
> >
> > Replace int with size_t for clarity and remove the
> > 'NEEDSWORK' tag associated with it.
> >
> > Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
> > ---
> > sideband.c: replace int with size_t for clarity
> >
> > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v1
> > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v1
> > Pull-Request: https://github.com/gitgitgadget/git/pull/1625
> >
> > sideband.c | 3 +--
> > 1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/sideband.c b/sideband.c
> > index 6cbfd391c47..1599e408d1b 100644
> > --- a/sideband.c
> > +++ b/sideband.c
> > @@ -69,9 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
> > * of the line. This should be called for a single line only, which is
> > * passed as the first N characters of the SRC array.
> > *
> > - * NEEDSWORK: use "size_t n" instead for clarity.
> > */
> > -static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> > +static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
> > {
> > int i;
> >
>
> Thanks for working on this.
> There is, however, more potential for improvements.
> First of all: the "int i" from above.
> Further down, we read
> for (i = 0; i < ARRAY_SIZE(keywords); i++) {
>
> However, a size of an array can never be negative, so that
> an unsigned data type is a better choice than a signed.
> And, arrays can have more elements than an int can address,
> at least in theory.
> For a reader it makes more sense, to replace
> int i;
> with
> size_t i;
>
>
> And further down, there is another place for improvments:
>
> int len = strlen(p->keyword);
> if (n < len)
> continue;
>
> Even here, a strlen is never negative. And a size_t is the choice for len,
> since all "modern" implementations declare strlen() to return size_t
>
> Do you think that you can have a look at these changes ?
>
> I will be happy to do a review, and possibly other people as well.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] sideband.c: replace int with size_t for clarity
2023-12-22 18:27 ` Chandra Pratap
@ 2023-12-22 18:39 ` Torsten Bögershausen
0 siblings, 0 replies; 12+ messages in thread
From: Torsten Bögershausen @ 2023-12-22 18:39 UTC (permalink / raw)
To: Chandra Pratap; +Cc: Chandra Pratap via GitGitGadget, git, Chandra Pratap
(PLease, avoid top-posting on this list)
On Fri, Dec 22, 2023 at 11:57:25PM +0530, Chandra Pratap wrote:
> Thanks for the feedback, Torsten. I was working on improving the rest of
> sideband.c as you suggested when I encountered this snippet on line 82:
>
> while (0 < n && isspace(*src)) {
> strbuf_addch(dest, *src);
> src++;
> n--;
> }
>
> Here, we are decreasing the value of an unsigned type to potentially below
> 0 which may lead to overflow and result in some nasty bug. In this case,
> is it wise to continue with replacing 'int n' with 'size_t n' as the
> NEEDSWORK tag suggests or should we improve upon the rest of the file
> and revert 'size_t n' to 'int n' ?
Yes, that could be a solution.
But.
In general, we are are striving to use size_t for all objects that live in
memory, and that is a long term thing.
Careful review is needed, and that is what you just did.
If we look at this code again:
while (0 < n && isspace(*src)) {
strbuf_addch(dest, *src);
src++;
n--;
}
When n is signed, it makes sense to use "0 < n".
However, if I think about it, it should work for unsigned as well,
wouldn't it ?
We can leave it as is, or replace it by
while (n && isspace(*src)) {
strbuf_addch(dest, *src);
src++;
n--;
}
>
> On Fri, 22 Dec 2023 at 23:27, Torsten Bögershausen <tboegi@web.de> wrote:
> >
> > On Fri, Dec 22, 2023 at 05:01:09PM +0000, Chandra Pratap via GitGitGadget wrote:
> > > From: Chandra Pratap <chandrapratap3519@gmail.com>
> > >
> > > Replace int with size_t for clarity and remove the
> > > 'NEEDSWORK' tag associated with it.
> > >
> > > Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
> > > ---
> > > sideband.c: replace int with size_t for clarity
> > >
> > > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v1
> > > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v1
> > > Pull-Request: https://github.com/gitgitgadget/git/pull/1625
> > >
> > > sideband.c | 3 +--
> > > 1 file changed, 1 insertion(+), 2 deletions(-)
> > >
> > > diff --git a/sideband.c b/sideband.c
> > > index 6cbfd391c47..1599e408d1b 100644
> > > --- a/sideband.c
> > > +++ b/sideband.c
> > > @@ -69,9 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
> > > * of the line. This should be called for a single line only, which is
> > > * passed as the first N characters of the SRC array.
> > > *
> > > - * NEEDSWORK: use "size_t n" instead for clarity.
> > > */
> > > -static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> > > +static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
> > > {
> > > int i;
> > >
> >
> > Thanks for working on this.
> > There is, however, more potential for improvements.
> > First of all: the "int i" from above.
> > Further down, we read
> > for (i = 0; i < ARRAY_SIZE(keywords); i++) {
> >
> > However, a size of an array can never be negative, so that
> > an unsigned data type is a better choice than a signed.
> > And, arrays can have more elements than an int can address,
> > at least in theory.
> > For a reader it makes more sense, to replace
> > int i;
> > with
> > size_t i;
> >
> >
> > And further down, there is another place for improvments:
> >
> > int len = strlen(p->keyword);
> > if (n < len)
> > continue;
> >
> > Even here, a strlen is never negative. And a size_t is the choice for len,
> > since all "modern" implementations declare strlen() to return size_t
> >
> > Do you think that you can have a look at these changes ?
> >
> > I will be happy to do a review, and possibly other people as well.
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] sideband.c: replace int with size_t for clarity
2023-12-22 17:57 ` Torsten Bögershausen
2023-12-22 18:27 ` Chandra Pratap
@ 2023-12-22 19:01 ` Junio C Hamano
2024-01-02 22:31 ` Taylor Blau
1 sibling, 1 reply; 12+ messages in thread
From: Junio C Hamano @ 2023-12-22 19:01 UTC (permalink / raw)
To: Torsten Bögershausen
Cc: Chandra Pratap via GitGitGadget, git, Chandra Pratap,
Chandra Pratap
Torsten Bögershausen <tboegi@web.de> writes:
Just this part.
> Further down, we read
> for (i = 0; i < ARRAY_SIZE(keywords); i++) {
>
> However, a size of an array can never be negative, so that
> an unsigned data type is a better choice than a signed.
> And, arrays can have more elements than an int can address,
> at least in theory.
> For a reader it makes more sense, to replace
> int i;
> with
> size_t i;
It is a very good discipline to use size_t to index into an array
whose size is externally controled (e.g., we slurp what the end user
or the server on the other end of the connection gave us into a
piece of memory we allocate) to avoid integer overflows as "int" is
often narrower than "size_t". But this particular one is a Meh; the
keywords[] is a small hardcoded array whose size and contents are
totally under our control.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2] sideband.c: replace int with size_t for clarity
2023-12-22 17:01 [PATCH] sideband.c: replace int with size_t for clarity Chandra Pratap via GitGitGadget
2023-12-22 17:57 ` Torsten Bögershausen
@ 2023-12-23 17:03 ` Chandra Pratap via GitGitGadget
2023-12-27 10:20 ` [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag Chandra Pratap via GitGitGadget
2023-12-26 17:14 ` [PATCH] sideband.c: replace int with size_t for clarity Junio C Hamano
2 siblings, 1 reply; 12+ messages in thread
From: Chandra Pratap via GitGitGadget @ 2023-12-23 17:03 UTC (permalink / raw)
To: git; +Cc: Torsten Bögershausen, Chandra Pratap, Chandra Pratap
From: Chandra Pratap <chandrapratap3519@gmail.com>
Replace int with size_t for non-negative values to improve
clarity and remove the 'NEEDSWORK' tag associated with it.wq
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
sideband.c: replace int with size_t for clarity
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1625
Range-diff vs v1:
1: be67e1bca38 ! 1: fdacc69ae3b sideband.c: replace int with size_t for clarity
@@ Metadata
## Commit message ##
sideband.c: replace int with size_t for clarity
- Replace int with size_t for clarity and remove the
- 'NEEDSWORK' tag associated with it.
+ Replace int with size_t for non-negative values to improve
+ clarity and remove the 'NEEDSWORK' tag associated with it.wq
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
@@ sideband.c: void list_config_color_sideband_slots(struct string_list *list, cons
{
int i;
+@@ sideband.c: static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+
+ for (i = 0; i < ARRAY_SIZE(keywords); i++) {
+ struct keyword_entry *p = keywords + i;
+- int len = strlen(p->keyword);
++ size_t len = strlen(p->keyword);
+
+ if (n < len)
+ continue;
sideband.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/sideband.c b/sideband.c
index 6cbfd391c47..7c3ec0ece29 100644
--- a/sideband.c
+++ b/sideband.c
@@ -69,9 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
* of the line. This should be called for a single line only, which is
* passed as the first N characters of the SRC array.
*
- * NEEDSWORK: use "size_t n" instead for clarity.
*/
-static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
{
int i;
@@ -88,7 +87,7 @@ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
for (i = 0; i < ARRAY_SIZE(keywords); i++) {
struct keyword_entry *p = keywords + i;
- int len = strlen(p->keyword);
+ size_t len = strlen(p->keyword);
if (n < len)
continue;
base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
--
gitgitgadget
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH] sideband.c: replace int with size_t for clarity
2023-12-22 17:01 [PATCH] sideband.c: replace int with size_t for clarity Chandra Pratap via GitGitGadget
2023-12-22 17:57 ` Torsten Bögershausen
2023-12-23 17:03 ` [PATCH v2] " Chandra Pratap via GitGitGadget
@ 2023-12-26 17:14 ` Junio C Hamano
2 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2023-12-26 17:14 UTC (permalink / raw)
To: Chandra Pratap via GitGitGadget; +Cc: git, Chandra Pratap, Chandra Pratap
"Chandra Pratap via GitGitGadget" <gitgitgadget@gmail.com> writes:
> -static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> +static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
Changing the type of the paramter alone might be a good start, but
does not really help anybody, as (1) the callers are not taught to
handle wider integral types for the values they pass and (2) the
function uses "int len" it computes internally to compare with "n".
There are three callers in demultiplex_sideband(), one of whose
paramters is "int len" and is passed to this function in one of
these calls. Among the other two, one uses "int linelen" derived
from scanning the piece of memory read from sideband via strpbrk(),
and the other uses strlen(b) which is the length of the "remainder"
of the same buffer after the loop that processes one line at a time
using the said strpbrk() is done with the complete lines in the
early part.
The buffer involved in all of the above stores bytes taken from a
packet received via the pkt-line interface, which is capable of
transferring only 64kB at a time.
I _think_ the most productive use of our time is to replace the
NEEDSWORK with a comment saying why it is fine to use "int" here to
avoid tempting the next developer to come up with this patch
again---they will waste their time to do so without thinking it
through if we leave the (incomplete) NEEDSWORK comment, I am afraid.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag
2023-12-23 17:03 ` [PATCH v2] " Chandra Pratap via GitGitGadget
@ 2023-12-27 10:20 ` Chandra Pratap via GitGitGadget
2023-12-27 22:22 ` Junio C Hamano
2023-12-28 8:01 ` [PATCH v4] " Chandra Pratap via GitGitGadget
0 siblings, 2 replies; 12+ messages in thread
From: Chandra Pratap via GitGitGadget @ 2023-12-27 10:20 UTC (permalink / raw)
To: git; +Cc: Torsten Bögershausen, Chandra Pratap, Chandra Pratap
From: Chandra Pratap <chandrapratap3519@gmail.com>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
sideband.c: replace int with size_t for clarity
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1625
Range-diff vs v2:
1: fdacc69ae3b ! 1: 273415aa6a4 sideband.c: replace int with size_t for clarity
@@ Metadata
Author: Chandra Pratap <chandrapratap3519@gmail.com>
## Commit message ##
- sideband.c: replace int with size_t for clarity
-
- Replace int with size_t for non-negative values to improve
- clarity and remove the 'NEEDSWORK' tag associated with it.wq
+ sideband.c: remove redundant 'NEEDSWORK' tag
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
@@ sideband.c: void list_config_color_sideband_slots(struct string_list *list, cons
* passed as the first N characters of the SRC array.
*
- * NEEDSWORK: use "size_t n" instead for clarity.
++ * It is fine to use "int n" here instead of "size_t n" as all calls to this
++ * function pass an 'int' parameter.
*/
--static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
-+static void maybe_colorize_sideband(struct strbuf *dest, const char *src, size_t n)
+ static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
{
- int i;
-
-@@ sideband.c: static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
-
- for (i = 0; i < ARRAY_SIZE(keywords); i++) {
- struct keyword_entry *p = keywords + i;
-- int len = strlen(p->keyword);
-+ size_t len = strlen(p->keyword);
-
- if (n < len)
- continue;
sideband.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/sideband.c b/sideband.c
index 6cbfd391c47..a89201d52ac 100644
--- a/sideband.c
+++ b/sideband.c
@@ -69,7 +69,8 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
* of the line. This should be called for a single line only, which is
* passed as the first N characters of the SRC array.
*
- * NEEDSWORK: use "size_t n" instead for clarity.
+ * It is fine to use "int n" here instead of "size_t n" as all calls to this
+ * function pass an 'int' parameter.
*/
static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
{
base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
--
gitgitgadget
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag
2023-12-27 10:20 ` [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag Chandra Pratap via GitGitGadget
@ 2023-12-27 22:22 ` Junio C Hamano
2023-12-28 8:01 ` [PATCH v4] " Chandra Pratap via GitGitGadget
1 sibling, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2023-12-27 22:22 UTC (permalink / raw)
To: Chandra Pratap via GitGitGadget
Cc: git, Torsten Bögershausen, Chandra Pratap, Chandra Pratap
"Chandra Pratap via GitGitGadget" <gitgitgadget@gmail.com> writes:
> - * NEEDSWORK: use "size_t n" instead for clarity.
> ++ * It is fine to use "int n" here instead of "size_t n" as all calls to this
> ++ * function pass an 'int' parameter.
This does not sound like a sufficient justification, though.
We should also explain why "int" is good enough for these callers.
Otherwise, using size_t throughout the callchain would become
another viable solution.
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v4] sideband.c: remove redundant 'NEEDSWORK' tag
2023-12-27 10:20 ` [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag Chandra Pratap via GitGitGadget
2023-12-27 22:22 ` Junio C Hamano
@ 2023-12-28 8:01 ` Chandra Pratap via GitGitGadget
2023-12-28 20:33 ` Junio C Hamano
1 sibling, 1 reply; 12+ messages in thread
From: Chandra Pratap via GitGitGadget @ 2023-12-28 8:01 UTC (permalink / raw)
To: git; +Cc: Torsten Bögershausen, Chandra Pratap, Chandra Pratap
From: Chandra Pratap <chandrapratap3519@gmail.com>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
sideband.c: replace int with size_t for clarity
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/1625
Range-diff vs v3:
1: 273415aa6a4 ! 1: 8c003256e5b sideband.c: remove redundant 'NEEDSWORK' tag
@@ sideband.c: void list_config_color_sideband_slots(struct string_list *list, cons
*
- * NEEDSWORK: use "size_t n" instead for clarity.
+ * It is fine to use "int n" here instead of "size_t n" as all calls to this
-+ * function pass an 'int' parameter.
++ * function pass an 'int' parameter. Additionally, the buffer involved in
++ * storing these 'int' values takes input from a packet via the pkt-line
++ * interface, which is capable of transferring only 64kB at a time.
*/
static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
{
sideband.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/sideband.c b/sideband.c
index 6cbfd391c47..266a67342be 100644
--- a/sideband.c
+++ b/sideband.c
@@ -69,7 +69,10 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
* of the line. This should be called for a single line only, which is
* passed as the first N characters of the SRC array.
*
- * NEEDSWORK: use "size_t n" instead for clarity.
+ * It is fine to use "int n" here instead of "size_t n" as all calls to this
+ * function pass an 'int' parameter. Additionally, the buffer involved in
+ * storing these 'int' values takes input from a packet via the pkt-line
+ * interface, which is capable of transferring only 64kB at a time.
*/
static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
{
base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
--
gitgitgadget
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v4] sideband.c: remove redundant 'NEEDSWORK' tag
2023-12-28 8:01 ` [PATCH v4] " Chandra Pratap via GitGitGadget
@ 2023-12-28 20:33 ` Junio C Hamano
0 siblings, 0 replies; 12+ messages in thread
From: Junio C Hamano @ 2023-12-28 20:33 UTC (permalink / raw)
To: Chandra Pratap via GitGitGadget
Cc: git, Torsten Bögershausen, Chandra Pratap, Chandra Pratap
"Chandra Pratap via GitGitGadget" <gitgitgadget@gmail.com> writes:
> Subject: Re: [PATCH v4] sideband.c: remove redundant 'NEEDSWORK' tag
The reason for removal is not that it was redundant and we said the
same thing elsewhere. Rather, what it claimed to be necessary has
turned to be unwanted. So something like
Subject: sideband.c: update stale NEEDSWORK comment
If we really wanted to change the type of the parameter to this
function to "size_t", we should also update its callers to hold
the values they use to compute the parameter also in "size_t".
But in this callchain, "int" is wide enough. Avoid tempting
future developers into wasting their time on using "size_t"
around this function.
or along that line would be more appropriate, perhaps?
Thanks.
> From: Chandra Pratap <chandrapratap3519@gmail.com>
>
> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
> ---
> sideband.c: replace int with size_t for clarity
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1625%2FChand-ra%2Fdusra-v4
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1625/Chand-ra/dusra-v4
> Pull-Request: https://github.com/gitgitgadget/git/pull/1625
>
> Range-diff vs v3:
>
> 1: 273415aa6a4 ! 1: 8c003256e5b sideband.c: remove redundant 'NEEDSWORK' tag
> @@ sideband.c: void list_config_color_sideband_slots(struct string_list *list, cons
> *
> - * NEEDSWORK: use "size_t n" instead for clarity.
> + * It is fine to use "int n" here instead of "size_t n" as all calls to this
> -+ * function pass an 'int' parameter.
> ++ * function pass an 'int' parameter. Additionally, the buffer involved in
> ++ * storing these 'int' values takes input from a packet via the pkt-line
> ++ * interface, which is capable of transferring only 64kB at a time.
> */
> static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> {
>
>
> sideband.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/sideband.c b/sideband.c
> index 6cbfd391c47..266a67342be 100644
> --- a/sideband.c
> +++ b/sideband.c
> @@ -69,7 +69,10 @@ void list_config_color_sideband_slots(struct string_list *list, const char *pref
> * of the line. This should be called for a single line only, which is
> * passed as the first N characters of the SRC array.
> *
> - * NEEDSWORK: use "size_t n" instead for clarity.
> + * It is fine to use "int n" here instead of "size_t n" as all calls to this
> + * function pass an 'int' parameter. Additionally, the buffer involved in
> + * storing these 'int' values takes input from a packet via the pkt-line
> + * interface, which is capable of transferring only 64kB at a time.
> */
> static void maybe_colorize_sideband(struct strbuf *dest, const char *src, int n)
> {
>
> base-commit: 1a87c842ece327d03d08096395969aca5e0a6996
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH] sideband.c: replace int with size_t for clarity
2023-12-22 19:01 ` Junio C Hamano
@ 2024-01-02 22:31 ` Taylor Blau
0 siblings, 0 replies; 12+ messages in thread
From: Taylor Blau @ 2024-01-02 22:31 UTC (permalink / raw)
To: Junio C Hamano
Cc: Torsten Bögershausen, Chandra Pratap via GitGitGadget, git,
Chandra Pratap, Chandra Pratap
On Fri, Dec 22, 2023 at 11:01:37AM -0800, Junio C Hamano wrote:
> Torsten Bögershausen <tboegi@web.de> writes:
>
> Just this part.
>
> > Further down, we read
> > for (i = 0; i < ARRAY_SIZE(keywords); i++) {
> >
> > However, a size of an array can never be negative, so that
> > an unsigned data type is a better choice than a signed.
> > And, arrays can have more elements than an int can address,
> > at least in theory.
> > For a reader it makes more sense, to replace
> > int i;
> > with
> > size_t i;
>
> It is a very good discipline to use size_t to index into an array
> whose size is externally controled (e.g., we slurp what the end user
> or the server on the other end of the connection gave us into a
> piece of memory we allocate) to avoid integer overflows as "int" is
> often narrower than "size_t". But this particular one is a Meh; the
> keywords[] is a small hardcoded array whose size and contents are
> totally under our control.
I certainly agree in theory, though I've always erred on the side of
always using size_t for indexing into arrays, even if they're small. It
removes a potential pitfall if you are working with an
externally-controlled array and happen to forget to use size_t.
But if there is an existing index variable with type "int", and we can
easily validate that it's small, I probably wouldn't bother changing it
if I was editing nearby code.
Thanks,
Taylor
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2024-01-02 22:31 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-22 17:01 [PATCH] sideband.c: replace int with size_t for clarity Chandra Pratap via GitGitGadget
2023-12-22 17:57 ` Torsten Bögershausen
2023-12-22 18:27 ` Chandra Pratap
2023-12-22 18:39 ` Torsten Bögershausen
2023-12-22 19:01 ` Junio C Hamano
2024-01-02 22:31 ` Taylor Blau
2023-12-23 17:03 ` [PATCH v2] " Chandra Pratap via GitGitGadget
2023-12-27 10:20 ` [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag Chandra Pratap via GitGitGadget
2023-12-27 22:22 ` Junio C Hamano
2023-12-28 8:01 ` [PATCH v4] " Chandra Pratap via GitGitGadget
2023-12-28 20:33 ` Junio C Hamano
2023-12-26 17:14 ` [PATCH] sideband.c: replace int with size_t for clarity 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).