* [PATCH 3/3] Use stringbuf to clean up some string handling code.
@ 2007-05-20 2:25 Timo Sirainen
2007-05-20 9:56 ` Alex Riesen
0 siblings, 1 reply; 4+ messages in thread
From: Timo Sirainen @ 2007-05-20 2:25 UTC (permalink / raw)
To: git
[-- Attachment #1: Type: text/plain, Size: 4198 bytes --]
---
commit.c | 30 +++++++++++++-----------------
local-fetch.c | 34 ++++++++++++++++------------------
2 files changed, 29 insertions(+), 35 deletions(-)
diff --git a/commit.c b/commit.c
index bee066f..58f1718 100644
--- a/commit.c
+++ b/commit.c
@@ -6,6 +6,7 @@
#include "interpolate.h"
#include "diff.h"
#include "revision.h"
+#include "str.h"
int save_commit_buffer = 1;
@@ -821,7 +822,7 @@ static long format_commit_message(const struct
commit *commit,
ILEFT_RIGHT,
};
struct commit_list *p;
- char parents[1024];
+ stringbuf(parents, 1024);
int i;
enum { HEADER, SUBJECT, BODY } state;
@@ -853,22 +854,17 @@ static long format_commit_message(const struct
commit *commit,
? "<"
: ">");
- parents[1] = 0;
- for (i = 0, p = commit->parents;
- p && i < sizeof(parents) - 1;
- p = p->next)
- i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
- sha1_to_hex(p->item->object.sha1));
- interp_set_entry(table, IPARENTS, parents + 1);
-
- parents[1] = 0;
- for (i = 0, p = commit->parents;
- p && i < sizeof(parents) - 1;
- p = p->next)
- i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
- find_unique_abbrev(p->item->object.sha1,
- DEFAULT_ABBREV));
- interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
+ str_c(parents)[1] = 0;
+ for (p = commit->parents; p; p = p->next)
+ str_printfa(parents, " %s", sha1_to_hex(p->item->object.sha1));
+ interp_set_entry(table, IPARENTS, str_c(parents) + 1);
+
+ str_c(parents)[1] = 0;
+ for (p = commit->parents; p; p = p->next)
+ str_printfa(parents, " %s",
+ find_unique_abbrev(p->item->object.sha1,
+ DEFAULT_ABBREV));
+ interp_set_entry(table, IPARENTS_ABBREV, str_c(parents) + 1);
for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
int eol;
diff --git a/local-fetch.c b/local-fetch.c
index 4b650ef..6d0599f 100644
--- a/local-fetch.c
+++ b/local-fetch.c
@@ -4,6 +4,7 @@
#include "cache.h"
#include "commit.h"
#include "fetch.h"
+#include "str.h"
static int use_link;
static int use_symlink;
@@ -21,12 +22,11 @@ static struct packed_git *packs;
static void setup_index(unsigned char *sha1)
{
struct packed_git *new_pack;
- char filename[PATH_MAX];
- strcpy(filename, path);
- strcat(filename, "/objects/pack/pack-");
- strcat(filename, sha1_to_hex(sha1));
- strcat(filename, ".idx");
- new_pack = parse_pack_index_file(sha1, filename);
+ stringbuf(filename, PATH_MAX);
+
+ str_printfa(filename, "%s/objects/pack/pack-%s.idx",
+ path, sha1_to_hex(sha1));
+ new_pack = parse_pack_index_file(sha1, str_c(filename));
new_pack->next = packs;
packs = new_pack;
}
@@ -35,10 +35,11 @@ static int setup_indices(void)
{
DIR *dir;
struct dirent *de;
- char filename[PATH_MAX];
+ stringbuf(filename, PATH_MAX);
unsigned char sha1[20];
- sprintf(filename, "%s/objects/pack/", path);
- dir = opendir(filename);
+
+ str_printfa(filename, "%s/objects/pack/", path);
+ dir = opendir(str_c(filename));
if (!dir)
return -1;
while ((de = readdir(dir)) != NULL) {
@@ -137,20 +138,17 @@ static int fetch_pack(const unsigned char *sha1)
static int fetch_file(const unsigned char *sha1)
{
static int object_name_start = -1;
- static char filename[PATH_MAX];
+ static stringbuf(filename, PATH_MAX);
char *hex = sha1_to_hex(sha1);
char *dest_filename = sha1_file_name(sha1);
if (object_name_start < 0) {
- strcpy(filename, path); /* e.g. git.git */
- strcat(filename, "/objects/");
- object_name_start = strlen(filename);
+ str_printfa(filename, "%s/objects/", path); /* e.g. git.git */
+ object_name_start = str_len(filename);
}
- filename[object_name_start+0] = hex[0];
- filename[object_name_start+1] = hex[1];
- filename[object_name_start+2] = '/';
- strcpy(filename + object_name_start + 3, hex + 2);
- return copy_file(filename, dest_filename, hex, 0);
+ str_truncate(filename, object_name_start);
+ str_printfa(filename, "%c%c/%s", hex[0], hex[1], hex + 2);
+ return copy_file(str_c(filename), dest_filename, hex, 0);
}
int fetch(unsigned char *sha1)
--
1.5.1.4
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 3/3] Use stringbuf to clean up some string handling code.
2007-05-20 2:25 [PATCH 3/3] Use stringbuf to clean up some string handling code Timo Sirainen
@ 2007-05-20 9:56 ` Alex Riesen
2007-05-20 10:04 ` Junio C Hamano
2007-05-20 11:19 ` Timo Sirainen
0 siblings, 2 replies; 4+ messages in thread
From: Alex Riesen @ 2007-05-20 9:56 UTC (permalink / raw)
To: Timo Sirainen; +Cc: git
Timo Sirainen, Sun, May 20, 2007 04:25:42 +0200:
> ---
> commit.c | 30 +++++++++++++-----------------
> local-fetch.c | 34 ++++++++++++++++------------------
> 2 files changed, 29 insertions(+), 35 deletions(-)
I find it hard to believe that it actually was a cleanup.
It is a nicer code, but... it is bigger, heavier on stack, and it does
not actually fix anything.
In my experience, such changes are seldom worth the effort. It may be
a nice code (and I actually like str.[hc]), but its use _must_ be
justified. I.e. it must simplify a complex formatting routine, or fix
a bug, which otherwise would be too hard or ugly to fix. It is
definitely not the case in this patch.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 3/3] Use stringbuf to clean up some string handling code.
2007-05-20 9:56 ` Alex Riesen
@ 2007-05-20 10:04 ` Junio C Hamano
2007-05-20 11:19 ` Timo Sirainen
1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2007-05-20 10:04 UTC (permalink / raw)
To: Alex Riesen; +Cc: Timo Sirainen, git
Alex Riesen <raa.lkml@gmail.com> writes:
> Timo Sirainen, Sun, May 20, 2007 04:25:42 +0200:
>> ---
>> commit.c | 30 +++++++++++++-----------------
>> local-fetch.c | 34 ++++++++++++++++------------------
>> 2 files changed, 29 insertions(+), 35 deletions(-)
>
> I find it hard to believe that it actually was a cleanup.
>
> It is a nicer code, but... it is bigger, heavier on stack, and it does
> not actually fix anything.
>
> In my experience, such changes are seldom worth the effort. It may be
> a nice code (and I actually like str.[hc]), but its use _must_ be
> justified. I.e. it must simplify a complex formatting routine, or fix
> a bug, which otherwise would be too hard or ugly to fix. It is
> definitely not the case in this patch.
Thanks. I was kind of waiting for somebody to say that for me
;-)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 3/3] Use stringbuf to clean up some string handling code.
2007-05-20 9:56 ` Alex Riesen
2007-05-20 10:04 ` Junio C Hamano
@ 2007-05-20 11:19 ` Timo Sirainen
1 sibling, 0 replies; 4+ messages in thread
From: Timo Sirainen @ 2007-05-20 11:19 UTC (permalink / raw)
To: Alex Riesen; +Cc: git
[-- Attachment #1: Type: text/plain, Size: 1477 bytes --]
On Sun, 2007-05-20 at 11:56 +0200, Alex Riesen wrote:
> Timo Sirainen, Sun, May 20, 2007 04:25:42 +0200:
> > ---
> > commit.c | 30 +++++++++++++-----------------
> > local-fetch.c | 34 ++++++++++++++++------------------
> > 2 files changed, 29 insertions(+), 35 deletions(-)
>
> I find it hard to believe that it actually was a cleanup.
>
> It is a nicer code, but... it is bigger, heavier on stack, and it does
> not actually fix anything.
>
> In my experience, such changes are seldom worth the effort. It may be
> a nice code (and I actually like str.[hc]), but its use _must_ be
> justified. I.e. it must simplify a complex formatting routine, or fix
> a bug, which otherwise would be too hard or ugly to fix. It is
> definitely not the case in this patch.
In my own projects security is the highest priority and it justifies
pretty much all changes. I've done several large changes that change
thousands of lines of code just because it makes it a bit easier to
verify the code's safety/correctness.
I realize that other projects may not want to use all of the tricks that
I'm using in my C code (type safe dynamic arrays, type safe context
pointer in callback functions, etc.), but I was hoping that at least the
libc string handling functions would never be used in a large project
anymore. Using them makes it extremely time consuming to verify the
code's safety, and at least I try to avoid software if I can't do that.
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-05-20 11:19 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-20 2:25 [PATCH 3/3] Use stringbuf to clean up some string handling code Timo Sirainen
2007-05-20 9:56 ` Alex Riesen
2007-05-20 10:04 ` Junio C Hamano
2007-05-20 11:19 ` Timo Sirainen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox