Skip to contentSkip to conversation

sideband.c: replace int with size_t for clarity

PATCH Superseded Chandra Pratap via GitGitGadget 5 participants 12 messages Dec 22, 2023 – Jan 2, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sideband.c: replace int with size_t for clarity

From: Chandra Pratap <[email protected]>

Replace int with size_t for clarity and remove the 'NEEDSWORK' tag associated with it.

---
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(-)
sideband.c +1 -2
index 6cbfd391c47..1599e408d1b 100644
@@ -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

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 21 lines · Chandra Pratap via GitGitGadget

From: Chandra Pratap <[email protected]>

Replace int with size_t for clarity and remove the 'NEEDSWORK' tag associated with it.

---
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(-)
sideband.c +0 -0
index 6cbfd391c47..1599e408d1b 100644

sideband.c void list_config_color_sideband_slots(struct string_list *list, const char *pref · line 76

Quoted 10 lines
sideband.c +1 -2
@@ -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.

#2 lore raw

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' ?

Quoted 62 lines · Torsten Bögershausen
Quoted 21 lines · Chandra Pratap via GitGitGadget

From: Chandra Pratap <[email protected]>

Replace int with size_t for clarity and remove the 'NEEDSWORK' tag associated with it.

---
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(-)
sideband.c +0 -0
index 6cbfd391c47..1599e408d1b 100644

sideband.c void list_config_color_sideband_slots(struct string_list *list, const char *pref · line 76

Quoted 10 lines
sideband.c +1 -2
@@ -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.

#3 lore raw

(PLease, avoid top-posting on this list)

Quoted 9 lines · 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--;
}

Quoted 5 lines

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--;
}

Quoted 65 lines · Torsten Bögershausen
Quoted 62 lines · Chandra Pratap via GitGitGadget
Quoted 21 lines

From: Chandra Pratap <[email protected]>

Replace int with size_t for clarity and remove the 'NEEDSWORK' tag associated with it.

---
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(-)
sideband.c +0 -0
index 6cbfd391c47..1599e408d1b 100644

sideband.c void list_config_color_sideband_slots(struct string_list *list, const char *pref · line 76

Quoted 10 lines
sideband.c +1 -2
@@ -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.

#4 lore raw
In reply to Torsten Bögershausen

Torsten Bögershausen <[email protected]> writes:

Just this part.

Quoted 11 lines

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.

#5 lore raw
Quoted 16 lines · Junio C Hamano

Torsten Bögershausen <[email protected]> writes:

Just this part.

Quoted 11 lines

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;

Quoted 7 lines

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

#6 lore raw

PATCH v2 1/1 sideband.c: replace int with size_t for clarity

In reply to Chandra Pratap via GitGitGadget

From: Chandra Pratap <[email protected]>

Replace int with size_t for non-negative values to improve clarity and remove the 'NEEDSWORK' tag associated with it.wq

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

@@ 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(-)
sideband.c +2 -3
index 6cbfd391c47..7c3ec0ece29 100644
@@ -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

Signature · 1 line
gitgitgadget
#7 lore raw

PATCH v3 1/1 sideband.c: remove redundant 'NEEDSWORK' tag

From: Chandra Pratap <[email protected]>

---
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 <[email protected]>

## 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

@@ 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(-)
sideband.c +2 -1
index 6cbfd391c47..a89201d52ac 100644
@@ -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

Signature · 1 line
gitgitgadget
#8 lore raw

Re: [PATCH v3] sideband.c: remove redundant 'NEEDSWORK' tag

Quoted 3 lines · Chandra Pratap via GitGitGadget

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

#9 lore raw

PATCH v4 1/1 sideband.c: remove redundant 'NEEDSWORK' tag

In reply to Chandra Pratap via GitGitGadget

From: Chandra Pratap <[email protected]>

---
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(-)
sideband.c +4 -1
index 6cbfd391c47..266a67342be 100644
@@ -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

Signature · 1 line
gitgitgadget
#10 lore raw

Re: [PATCH v4] sideband.c: remove redundant 'NEEDSWORK' tag

Quoted 1 line · Chandra Pratap via GitGitGadget

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.

Quoted 47 lines

From: Chandra Pratap <[email protected]>

---
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(-)
sideband.c +4 -1
index 6cbfd391c47..266a67342be 100644
@@ -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

#11 lore raw
In reply to Chandra Pratap via GitGitGadget

sideband.c void list_config_color_sideband_slots(struct string_list *list, const char *pref · line 73

Quoted 2 lines · Chandra Pratap via GitGitGadget
sideband.c +1 -1
-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.