@matheusbsbs / matheus-bagliano / commits / 817151e61a
Fork 0

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 <s022018@student.dtu.dk> Signed-off-by: Junio C Hamano <junkio@cox.net>

Peter Eriksen committed Jun 24, 2006 at 16:01 UTC 817151e61a74241df5b5dd206d27086283f28b84
12 files changed +48 -30
Makefile
+14
@@ -26,6 +26,8 @@ all:
26 #
27 # Define NO_STRCASESTR if you don't have strcasestr.
28 #
29 +# Define NO_STRLCPY if you don't have strlcpy.
30 +#
31 # Define NO_SETENV if you don't have setenv in the C library.
32 #
33 # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -240,9 +242,13 @@ LIBS = $(GITLIBS) -lz
242 # because maintaining the nesting to match is a pain. If
243 # we had "elif" things would have been much nicer...
244
245 +ifeq ($(uname_S),Linux)
246 + NO_STRLCPY = YesPlease
247 +endif
248 ifeq ($(uname_S),Darwin)
249 NEEDS_SSL_WITH_CRYPTO = YesPlease
250 NEEDS_LIBICONV = YesPlease
251 + NO_STRLCPY = YesPlease
252 ## fink
253 ifeq ($(shell test -d /sw/lib && echo y),y)
254 ALL_CFLAGS += -I/sw/include
@@ -259,6 +265,7 @@ ifeq ($(uname_S),SunOS)
265 NEEDS_NSL = YesPlease
266 SHELL_PATH = /bin/bash
267 NO_STRCASESTR = YesPlease
268 + NO_STRLCPY = YesPlease
269 ifeq ($(uname_R),5.8)
270 NEEDS_LIBICONV = YesPlease
271 NO_UNSETENV = YesPlease
@@ -276,6 +283,7 @@ ifeq ($(uname_O),Cygwin)
283 NO_D_TYPE_IN_DIRENT = YesPlease
284 NO_D_INO_IN_DIRENT = YesPlease
285 NO_STRCASESTR = YesPlease
286 + NO_STRLCPY = YesPlease
287 NO_SYMLINK_HEAD = YesPlease
288 NEEDS_LIBICONV = YesPlease
289 # There are conflicting reports about this.
@@ -305,12 +313,14 @@ ifeq ($(uname_S),NetBSD)
313 endif
314 ifeq ($(uname_S),AIX)
315 NO_STRCASESTR=YesPlease
316 + NO_STRLCPY = YesPlease
317 NEEDS_LIBICONV=YesPlease
318 endif
319 ifeq ($(uname_S),IRIX64)
320 NO_IPV6=YesPlease
321 NO_SETENV=YesPlease
322 NO_STRCASESTR=YesPlease
323 + NO_STRLCPY = YesPlease
324 NO_SOCKADDR_STORAGE=YesPlease
325 SHELL_PATH=/usr/gnu/bin/bash
326 ALL_CFLAGS += -DPATH_MAX=1024
@@ -403,6 +413,10 @@ ifdef NO_STRCASESTR
413 COMPAT_CFLAGS += -DNO_STRCASESTR
414 COMPAT_OBJS += compat/strcasestr.o
415 endif
416 +ifdef NO_STRLCPY
417 + COMPAT_CFLAGS += -DNO_STRLCPY
418 + COMPAT_OBJS += compat/strlcpy.o
419 +endif
420 ifdef NO_SETENV
421 COMPAT_CFLAGS += -DNO_SETENV
422 COMPAT_OBJS += compat/setenv.o
builtin-log.c
+1 -1
@@ -115,7 +115,7 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
115 int len = 0;
116
117 if (output_directory) {
118 - safe_strncpy(filename, output_directory, 1010);
118 + strlcpy(filename, output_directory, 1010);
119 len = strlen(filename);
120 if (filename[len - 1] != '/')
121 filename[len++] = '/';
builtin-tar-tree.c
+2 -2
@@ -233,8 +233,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
233 /* XXX: should we provide more meaningful info here? */
234 sprintf(header.uid, "%07o", 0);
235 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));
238 sprintf(header.devmajor, "%07o", 0);
239 sprintf(header.devminor, "%07o", 0);
240
cache.h
-1
@@ -216,7 +216,6 @@ enum sharedrepo {
216 int git_config_perm(const char *var, const char *value);
217 int adjust_shared_perm(const char *path);
218 int safe_create_leading_directories(char *path);
219 -size_t safe_strncpy(char *, const char *, size_t);
219 char *enter_repo(char *path, int strict);
220
221 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
compat/strlcpy.c new
+13
@@ -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
+3 -3
@@ -280,17 +280,17 @@ int git_default_config(const char *var, const char *value)
280 }
281
282 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));
284 return 0;
285 }
286
287 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));
289 return 0;
290 }
291
292 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));
294 return 0;
295 }
296
git-compat-util.h
+5
@@ -79,6 +79,11 @@ extern void gitunsetenv(const char *);
79 extern char *gitstrcasestr(const char *haystack, const char *needle);
80 #endif
81
82 +#ifdef NO_STRLCPY
83 +#define strlcpy gitstrlcpy
84 +extern size_t gitstrlcpy(char *, const char *, size_t);
85 +#endif
86 +
87 static inline void *xmalloc(size_t size)
88 {
89 void *ret = malloc(size);
http-fetch.c
+3 -3
@@ -584,8 +584,8 @@ static void process_alternates_response(void *callback_data)
584 // skip 'objects' at end
585 if (okay) {
586 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);
589 if (get_verbosely)
590 fprintf(stderr,
591 "Also look at %s\n", target);
@@ -727,7 +727,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
727 if (ctx->cdata)
728 free(ctx->cdata);
729 ctx->cdata = xmalloc(len + 1);
730 - safe_strncpy(ctx->cdata, s, len + 1);
730 + strlcpy(ctx->cdata, s, len + 1);
731 }
732
733 static int remote_ls(struct alt_base *repo, const char *path, int flags,
http-push.c
+3 -3
@@ -1271,7 +1271,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
1271 if (ctx->cdata)
1272 free(ctx->cdata);
1273 ctx->cdata = xmalloc(len + 1);
1274 - safe_strncpy(ctx->cdata, s, len + 1);
1274 + strlcpy(ctx->cdata, s, len + 1);
1275 }
1276
1277 static struct remote_lock *lock_remote(char *path, long timeout)
@@ -1473,7 +1473,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
1473 return;
1474 path += 8;
1475 obj_hex = xmalloc(strlen(path));
1476 - safe_strncpy(obj_hex, path, 3);
1476 + strlcpy(obj_hex, path, 3);
1477 strcpy(obj_hex + 2, path + 3);
1478 one_remote_object(obj_hex);
1479 free(obj_hex);
@@ -2172,7 +2172,7 @@ static void fetch_symref(char *path, char **symref, unsigned char *sha1)
2172 /* If it's a symref, set the refname; otherwise try for a sha1 */
2173 if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
2174 *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);
2176 } else {
2177 get_sha1_hex(buffer.buffer, sha1);
2178 }
ident.c
+2 -2
@@ -71,9 +71,9 @@ int setup_ident(void)
71 len = strlen(git_default_email);
72 git_default_email[len++] = '.';
73 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);
75 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);
77 }
78 /* And set the default date */
79 datestamp(git_default_date, sizeof(git_default_date));
path.c
+1 -14
@@ -77,25 +77,12 @@ int git_mkstemp(char *path, size_t len, const char *template)
77 pch += n;
78 }
79
80 - safe_strncpy(pch, template, len);
80 + strlcpy(pch, template, len);
81
82 return mkstemp(path);
83 }
84
85
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 -
86 int validate_symref(const char *path)
87 {
88 struct stat st;
sha1_name.c
+1 -1
@@ -262,7 +262,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
262 if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') {
263 int date_len = len - am - 3;
264 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);
266 at_time = approxidate(date_spec);
267 free(date_spec);
268 len = am;