Skip to content

Commit 2b7ca83

Browse files
meyeringgitster
authored andcommitted
use write_str_in_full helper to avoid literal string lengths
In 2d14d65 (Use a clearer style to issue commands to remote helpers, 2009-09-03) I happened to notice two changes like this: - write_in_full(helper->in, "list\n", 5); + + strbuf_addstr(&buf, "list\n"); + write_in_full(helper->in, buf.buf, buf.len); + strbuf_reset(&buf); IMHO, it would be better to define a new function, static inline ssize_t write_str_in_full(int fd, const char *str) { return write_in_full(fd, str, strlen(str)); } and then use it like this: - strbuf_addstr(&buf, "list\n"); - write_in_full(helper->in, buf.buf, buf.len); - strbuf_reset(&buf); + write_str_in_full(helper->in, "list\n"); Thus not requiring the added allocation, and still avoiding the maintenance risk of literal string lengths. These days, compilers are good enough that strlen("literal") imposes no run-time cost. Transformed via this: perl -pi -e \ 's/write_in_full\((.*?), (".*?"), \d+\)/write_str_in_full($1, $2)/'\ $(git grep -l 'write_in_full.*"') Signed-off-by: Jim Meyering <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 511a3fc commit 2b7ca83

File tree

7 files changed

+14
-9
lines changed

7 files changed

+14
-9
lines changed

builtin-fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static int quickfetch(struct ref *ref_map)
454454

455455
for (ref = ref_map; ref; ref = ref->next) {
456456
if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
457-
write_in_full(revlist.in, "\n", 1) < 0) {
457+
write_str_in_full(revlist.in, "\n") < 0) {
458458
if (errno != EPIPE && errno != EINVAL)
459459
error("failed write to rev-list: %s", strerror(errno));
460460
err = -1;

builtin-reflog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
362362
} else if (cmd->updateref &&
363363
(write_in_full(lock->lock_fd,
364364
sha1_to_hex(cb.last_kept_sha1), 40) != 40 ||
365-
write_in_full(lock->lock_fd, "\n", 1) != 1 ||
365+
write_str_in_full(lock->lock_fd, "\n") != 1 ||
366366
close_ref(lock) < 0)) {
367367
status |= error("Couldn't write %s",
368368
lock->lk->filename);

cache.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,13 +923,18 @@ extern const char *git_mailmap_file;
923923
extern void maybe_flush_or_die(FILE *, const char *);
924924
extern int copy_fd(int ifd, int ofd);
925925
extern int copy_file(const char *dst, const char *src, int mode);
926-
extern ssize_t read_in_full(int fd, void *buf, size_t count);
927-
extern ssize_t write_in_full(int fd, const void *buf, size_t count);
928926
extern void write_or_die(int fd, const void *buf, size_t count);
929927
extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
930928
extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg);
931929
extern void fsync_or_die(int fd, const char *);
932930

931+
extern ssize_t read_in_full(int fd, void *buf, size_t count);
932+
extern ssize_t write_in_full(int fd, const void *buf, size_t count);
933+
static inline ssize_t write_str_in_full(int fd, const char *str)
934+
{
935+
return write_in_full(fd, str, strlen(str));
936+
}
937+
933938
/* pager.c */
934939
extern void setup_pager(void);
935940
extern const char *pager_program;

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ int write_shallow_commits(int fd, int use_pack_protocol)
212212
else {
213213
if (write_in_full(fd, hex, 40) != 40)
214214
break;
215-
if (write_in_full(fd, "\n", 1) != 1)
215+
if (write_str_in_full(fd, "\n") != 1)
216216
break;
217217
}
218218
}

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ int git_config_set_multivar(const char *key, const char *value,
11161116
copy_end - copy_begin)
11171117
goto write_err_out;
11181118
if (new_line &&
1119-
write_in_full(fd, "\n", 1) != 1)
1119+
write_str_in_full(fd, "\n") != 1)
11201120
goto write_err_out;
11211121
}
11221122
copy_begin = store.offset[i];

rerere.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static int write_rr(struct string_list *rr, int out_fd)
6161
path = rr->items[i].string;
6262
length = strlen(path) + 1;
6363
if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
64-
write_in_full(out_fd, "\t", 1) != 1 ||
64+
write_str_in_full(out_fd, "\t") != 1 ||
6565
write_in_full(out_fd, path, length) != length)
6666
die("unable to write rerere record");
6767
}

upload-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static void receive_needs(void)
553553

554554
shallow_nr = 0;
555555
if (debug_fd)
556-
write_in_full(debug_fd, "#S\n", 3);
556+
write_str_in_full(debug_fd, "#S\n");
557557
for (;;) {
558558
struct object *o;
559559
unsigned char sha1_buf[20];
@@ -619,7 +619,7 @@ static void receive_needs(void)
619619
}
620620
}
621621
if (debug_fd)
622-
write_in_full(debug_fd, "#E\n", 3);
622+
write_str_in_full(debug_fd, "#E\n");
623623

624624
if (!use_sideband && daemon_mode)
625625
no_progress = 1;

0 commit comments

Comments
 (0)