Skip to content

Commit 817151e

Browse files
Peter EriksenJunio C Hamano
authored andcommitted
Rename safe_strncpy() to strlcpy().
This cleans up the use of safe_strncpy() even more. Since it has the same semantics as strlcpy() use this name instead. Also move the definition from inside path.c to its own file compat/strlcpy.c, and use it conditionally at compile time, since some platforms already has strlcpy(). It's included in the same way as compat/setenv.c. Signed-off-by: Peter Eriksen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3eaa38d commit 817151e

File tree

12 files changed

+48
-30
lines changed

12 files changed

+48
-30
lines changed

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ all:
2626
#
2727
# Define NO_STRCASESTR if you don't have strcasestr.
2828
#
29+
# Define NO_STRLCPY if you don't have strlcpy.
30+
#
2931
# Define NO_SETENV if you don't have setenv in the C library.
3032
#
3133
# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -240,9 +242,13 @@ LIBS = $(GITLIBS) -lz
240242
# because maintaining the nesting to match is a pain. If
241243
# we had "elif" things would have been much nicer...
242244

245+
ifeq ($(uname_S),Linux)
246+
NO_STRLCPY = YesPlease
247+
endif
243248
ifeq ($(uname_S),Darwin)
244249
NEEDS_SSL_WITH_CRYPTO = YesPlease
245250
NEEDS_LIBICONV = YesPlease
251+
NO_STRLCPY = YesPlease
246252
## fink
247253
ifeq ($(shell test -d /sw/lib && echo y),y)
248254
ALL_CFLAGS += -I/sw/include
@@ -259,6 +265,7 @@ ifeq ($(uname_S),SunOS)
259265
NEEDS_NSL = YesPlease
260266
SHELL_PATH = /bin/bash
261267
NO_STRCASESTR = YesPlease
268+
NO_STRLCPY = YesPlease
262269
ifeq ($(uname_R),5.8)
263270
NEEDS_LIBICONV = YesPlease
264271
NO_UNSETENV = YesPlease
@@ -276,6 +283,7 @@ ifeq ($(uname_O),Cygwin)
276283
NO_D_TYPE_IN_DIRENT = YesPlease
277284
NO_D_INO_IN_DIRENT = YesPlease
278285
NO_STRCASESTR = YesPlease
286+
NO_STRLCPY = YesPlease
279287
NO_SYMLINK_HEAD = YesPlease
280288
NEEDS_LIBICONV = YesPlease
281289
# There are conflicting reports about this.
@@ -305,12 +313,14 @@ ifeq ($(uname_S),NetBSD)
305313
endif
306314
ifeq ($(uname_S),AIX)
307315
NO_STRCASESTR=YesPlease
316+
NO_STRLCPY = YesPlease
308317
NEEDS_LIBICONV=YesPlease
309318
endif
310319
ifeq ($(uname_S),IRIX64)
311320
NO_IPV6=YesPlease
312321
NO_SETENV=YesPlease
313322
NO_STRCASESTR=YesPlease
323+
NO_STRLCPY = YesPlease
314324
NO_SOCKADDR_STORAGE=YesPlease
315325
SHELL_PATH=/usr/gnu/bin/bash
316326
ALL_CFLAGS += -DPATH_MAX=1024
@@ -403,6 +413,10 @@ ifdef NO_STRCASESTR
403413
COMPAT_CFLAGS += -DNO_STRCASESTR
404414
COMPAT_OBJS += compat/strcasestr.o
405415
endif
416+
ifdef NO_STRLCPY
417+
COMPAT_CFLAGS += -DNO_STRLCPY
418+
COMPAT_OBJS += compat/strlcpy.o
419+
endif
406420
ifdef NO_SETENV
407421
COMPAT_CFLAGS += -DNO_SETENV
408422
COMPAT_OBJS += compat/setenv.o

builtin-log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
115115
int len = 0;
116116

117117
if (output_directory) {
118-
safe_strncpy(filename, output_directory, 1010);
118+
strlcpy(filename, output_directory, 1010);
119119
len = strlen(filename);
120120
if (filename[len - 1] != '/')
121121
filename[len++] = '/';

builtin-tar-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
233233
/* XXX: should we provide more meaningful info here? */
234234
sprintf(header.uid, "%07o", 0);
235235
sprintf(header.gid, "%07o", 0);
236-
safe_strncpy(header.uname, "git", sizeof(header.uname));
237-
safe_strncpy(header.gname, "git", sizeof(header.gname));
236+
strlcpy(header.uname, "git", sizeof(header.uname));
237+
strlcpy(header.gname, "git", sizeof(header.gname));
238238
sprintf(header.devmajor, "%07o", 0);
239239
sprintf(header.devminor, "%07o", 0);
240240

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ enum sharedrepo {
216216
int git_config_perm(const char *var, const char *value);
217217
int adjust_shared_perm(const char *path);
218218
int safe_create_leading_directories(char *path);
219-
size_t safe_strncpy(char *, const char *, size_t);
220219
char *enter_repo(char *path, int strict);
221220

222221
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */

compat/strlcpy.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <string.h>
2+
3+
size_t gitstrlcpy(char *dest, const char *src, size_t size)
4+
{
5+
size_t ret = strlen(src);
6+
7+
if (size) {
8+
size_t len = (ret >= size) ? size - 1 : ret;
9+
memcpy(dest, src, len);
10+
dest[len] = '\0';
11+
}
12+
return ret;
13+
}

config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,17 @@ int git_default_config(const char *var, const char *value)
280280
}
281281

282282
if (!strcmp(var, "user.name")) {
283-
safe_strncpy(git_default_name, value, sizeof(git_default_name));
283+
strlcpy(git_default_name, value, sizeof(git_default_name));
284284
return 0;
285285
}
286286

287287
if (!strcmp(var, "user.email")) {
288-
safe_strncpy(git_default_email, value, sizeof(git_default_email));
288+
strlcpy(git_default_email, value, sizeof(git_default_email));
289289
return 0;
290290
}
291291

292292
if (!strcmp(var, "i18n.commitencoding")) {
293-
safe_strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
293+
strlcpy(git_commit_encoding, value, sizeof(git_commit_encoding));
294294
return 0;
295295
}
296296

git-compat-util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ extern void gitunsetenv(const char *);
7979
extern char *gitstrcasestr(const char *haystack, const char *needle);
8080
#endif
8181

82+
#ifdef NO_STRLCPY
83+
#define strlcpy gitstrlcpy
84+
extern size_t gitstrlcpy(char *, const char *, size_t);
85+
#endif
86+
8287
static inline void *xmalloc(size_t size)
8388
{
8489
void *ret = malloc(size);

http-fetch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ static void process_alternates_response(void *callback_data)
584584
// skip 'objects' at end
585585
if (okay) {
586586
target = xmalloc(serverlen + posn - i - 6);
587-
safe_strncpy(target, base, serverlen);
588-
safe_strncpy(target + serverlen, data + i, posn - i - 6);
587+
strlcpy(target, base, serverlen);
588+
strlcpy(target + serverlen, data + i, posn - i - 6);
589589
if (get_verbosely)
590590
fprintf(stderr,
591591
"Also look at %s\n", target);
@@ -727,7 +727,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
727727
if (ctx->cdata)
728728
free(ctx->cdata);
729729
ctx->cdata = xmalloc(len + 1);
730-
safe_strncpy(ctx->cdata, s, len + 1);
730+
strlcpy(ctx->cdata, s, len + 1);
731731
}
732732

733733
static int remote_ls(struct alt_base *repo, const char *path, int flags,

http-push.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
12711271
if (ctx->cdata)
12721272
free(ctx->cdata);
12731273
ctx->cdata = xmalloc(len + 1);
1274-
safe_strncpy(ctx->cdata, s, len + 1);
1274+
strlcpy(ctx->cdata, s, len + 1);
12751275
}
12761276

12771277
static struct remote_lock *lock_remote(char *path, long timeout)
@@ -1473,7 +1473,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
14731473
return;
14741474
path += 8;
14751475
obj_hex = xmalloc(strlen(path));
1476-
safe_strncpy(obj_hex, path, 3);
1476+
strlcpy(obj_hex, path, 3);
14771477
strcpy(obj_hex + 2, path + 3);
14781478
one_remote_object(obj_hex);
14791479
free(obj_hex);
@@ -2172,7 +2172,7 @@ static void fetch_symref(char *path, char **symref, unsigned char *sha1)
21722172
/* If it's a symref, set the refname; otherwise try for a sha1 */
21732173
if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
21742174
*symref = xmalloc(buffer.posn - 5);
2175-
safe_strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
2175+
strlcpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
21762176
} else {
21772177
get_sha1_hex(buffer.buffer, sha1);
21782178
}

ident.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ int setup_ident(void)
7171
len = strlen(git_default_email);
7272
git_default_email[len++] = '.';
7373
if (he && (domainname = strchr(he->h_name, '.')))
74-
safe_strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
74+
strlcpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
7575
else
76-
safe_strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
76+
strlcpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
7777
}
7878
/* And set the default date */
7979
datestamp(git_default_date, sizeof(git_default_date));

path.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,12 @@ int git_mkstemp(char *path, size_t len, const char *template)
7777
pch += n;
7878
}
7979

80-
safe_strncpy(pch, template, len);
80+
strlcpy(pch, template, len);
8181

8282
return mkstemp(path);
8383
}
8484

8585

86-
size_t safe_strncpy(char *dest, const char *src, size_t size)
87-
{
88-
size_t ret = strlen(src);
89-
90-
if (size) {
91-
size_t len = (ret >= size) ? size - 1 : ret;
92-
memcpy(dest, src, len);
93-
dest[len] = '\0';
94-
}
95-
return ret;
96-
}
97-
98-
9986
int validate_symref(const char *path)
10087
{
10188
struct stat st;

sha1_name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
262262
if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') {
263263
int date_len = len - am - 3;
264264
char *date_spec = xmalloc(date_len + 1);
265-
safe_strncpy(date_spec, str + am + 2, date_len + 1);
265+
strlcpy(date_spec, str + am + 2, date_len + 1);
266266
at_time = approxidate(date_spec);
267267
free(date_spec);
268268
len = am;

0 commit comments

Comments
 (0)