[PATCH] progress.c: avoid use of dynamic-sized array

Subsystems: the rest

DORMANTno replies

6 messages, 5 authors, 2016-06-15 · open the first message on its own page

[PATCH] progress.c: avoid use of dynamic-sized array

From: Boyd Lynn Gerber <hidden>
Date: 2016-06-15 22:44:42

Dynamically sized arrays are gcc and C99 construct.  Using them hurts
portability to older compilers, although using them is nice in this case
it is not desirable.  This patch removes the only use of the construct
in stop_progress_msg(); the function is about writing out a single line
of a message, and the existing callers of this function feed messages
of only bounded size anyway, so use of dynamic array is simply overkill.

Signed-off-by: Boyd Lynn Gerber <redacted>

--
Boyd Gerber [off-list ref]
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047

---
        Developer's Certificate of Origin 1.1

        By making a contribution to this project, I certify that:

        (a) The contribution was created in whole or in part by me and I
            have the right to submit it under the open source license
            indicated in the file; or

        (b) The contribution is based upon previous work that, to the best
            of my knowledge, is covered under an appropriate open source
            license and I have the right under that license to submit that
            work with modifications, whether created in whole or in part
            by me, under the same open source license (unless I am
            permitted to submit under a different license), as indicated
            in the file; or

        (c) The contribution was provided directly to me by some other
            person who certified (a), (b) or (c) and I have not modified
            it.

        (d) I understand and agree that this project and the contribution
            are public and that a record of the contribution (including all
            personal information I submit with it, including my sign-off) is
            maintained indefinitely and may be redistributed consistent with
            this project or the open source license(s) involved.

---
progress.c
diff --git a/progress.c b/progress.c
index d19f80c..55a8687 100644
--- a/progress.c
+++ b/progress.c
@@ -241,16 +241,21 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
 	*p_progress = NULL;
 	if (progress->last_value != -1) {
 		/* Force the last update */
-		char buf[strlen(msg) + 5];
+		char buf[128], *bufp;
+		size_t len = strlen(msg) + 5;
 		struct throughput *tp = progress->throughput;
+
+		bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
 		if (tp) {
 			unsigned int rate = !tp->avg_misecs ? 0 :
 					tp->avg_bytes / tp->avg_misecs;
 			throughput_string(tp, tp->curr_total, rate);
 		}
 		progress_update = 1;
-		sprintf(buf, ", %s.\n", msg);
-		display(progress, progress->last_value, buf);
+		sprintf(bufp, ", %s.\n", msg);
+		display(progress, progress->last_value, bufp);
+		if (buf != bufp)
+			free(bufp);
 	}
 	clear_progress_signal();
 	free(progress->throughput);
-- 
1.5.2.4


--
Boyd Gerber <gerberb@zenez.com>
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047

Re: [PATCH] progress.c: avoid use of dynamic-sized array

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:44:42

Hi,

On Sun, 8 Jun 2008, Boyd Lynn Gerber wrote:
Dynamically sized arrays are gcc and C99 construct.  Using them hurts
portability to older compilers, although using them is nice in this case
it is not desirable.  This patch removes the only use of the construct
in stop_progress_msg(); the function is about writing out a single line
of a message, and the existing callers of this function feed messages
of only bounded size anyway, so use of dynamic array is simply overkill.

Signed-off-by: Boyd Lynn Gerber <redacted>

--
Boyd Gerber [off-list ref]
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047

---
Do you really want to have your mail signature in the commit message, 
particularly because...
[...]

 	clear_progress_signal();
 	free(progress->throughput);
-- 
1.5.2.4


--
Boyd Gerber [off-list ref]
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
You repeat it at the end of the mail anyway?

The patch looks fine to me, though.

Ciao,
Dscho

Re: [PATCH] progress.c: avoid use of dynamic-sized array

From: Boyd Lynn Gerber <hidden>
Date: 2016-06-15 22:44:42

On Sun, 8 Jun 2008, Johannes Schindelin wrote:
On Sun, 8 Jun 2008, Boyd Lynn Gerber wrote:
quoted
Dynamically sized arrays are gcc and C99 construct.  Using them hurts
portability to older compilers, although using them is nice in this case
it is not desirable.  This patch removes the only use of the construct
in stop_progress_msg(); the function is about writing out a single line
of a message, and the existing callers of this function feed messages
of only bounded size anyway, so use of dynamic array is simply overkill.

Signed-off-by: Boyd Lynn Gerber <redacted>

--
Boyd Gerber [off-list ref]
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047

---
Do you really want to have your mail signature in the commit message, 
particularly because...
quoted
[...]

 	clear_progress_signal();
 	free(progress->throughput);
-- 
1.5.2.4


--
Boyd Gerber [off-list ref]
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047
You repeat it at the end of the mail anyway?

The patch looks fine to me, though.
I put it in because in the git repo it is not their.  Only in the email.  
It is fine to remove it.

--
Boyd Gerber [off-list ref]
ZENEZ	1042 East Fort Union #135, Midvale Utah  84047

Re: [PATCH] progress.c: avoid use of dynamic-sized array

From: Alex Riesen <hidden>
Date: 2016-06-15 22:44:42

Boyd Lynn Gerber, Sun, Jun 08, 2008 17:26:15 +0200:
 		/* Force the last update */
-		char buf[strlen(msg) + 5];
+		char buf[128], *bufp;
+		size_t len = strlen(msg) + 5;
 		struct throughput *tp = progress->throughput;
+
+		bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
It is just a temporary buffer, there is no urgent need to
micro-optimize the allocation like that. Maybe just leave
one of the buffers, the one on stack? It is just a progress
message, snprintf will cut it, yes, but it is unlikely to
cause any harm (unless you forget to \n-terminate it).

Re: [PATCH] progress.c: avoid use of dynamic-sized array

From: しらいしななこ <hidden>
Date: 2016-06-15 22:44:42

Quoting Boyd Lynn Gerber [off-list ref]:
Dynamically sized arrays are gcc and C99 construct.  Using them hurts
portability to older compilers, although using them is nice in this case
it is not desirable.  This patch removes the only use of the construct
in stop_progress_msg(); the function is about writing out a single line
of a message, and the existing callers of this function feed messages
of only bounded size anyway, so use of dynamic array is simply overkill.

Signed-off-by: Boyd Lynn Gerber <redacted>
I may be mistaken but isn't this Junio's patch?  If so (quoting
from SubmittingPatches document):

  If you are forwarding a patch from somebody else, optionally, at
  the beginning of the e-mail message just before the commit
  message starts, you can put a "From: " line to name that person.

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

----------------------------------------------------------------------
Find out how you can get spam free email.
http://www.bluebottle.com/tag/3

Re: [PATCH] progress.c: avoid use of dynamic-sized array

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:44:42

しらいしななこ  [off-list ref] writes:
Quoting Boyd Lynn Gerber [off-list ref]:
quoted
Dynamically sized arrays are gcc and C99 construct.  Using them hurts
portability to older compilers, although using them is nice in this case
it is not desirable.  This patch removes the only use of the construct
in stop_progress_msg(); the function is about writing out a single line
of a message, and the existing callers of this function feed messages
of only bounded size anyway, so use of dynamic array is simply overkill.

Signed-off-by: Boyd Lynn Gerber <redacted>
I may be mistaken but isn't this Junio's patch?  If so (quoting
from SubmittingPatches document):

  If you are forwarding a patch from somebody else, optionally, at
  the beginning of the e-mail message just before the commit
  message starts, you can put a "From: " line to name that person.
Heh, thanks for being picky ;-)

Something this small, either way is fine by me.  Besides, I've applied the
patch (and the other one) from Boyd already.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help