Implement safe_strncpy() as strlcpy() and use it more.
Signed-off-by: Peter Eriksen <s022018@student.dtu.dk> Signed-off-by: Junio C Hamano <junkio@cox.net>
Peter Eriksen committed
Jun 11, 2006 at 14:03 UTC
bfbd0bb6ecbbbf75a5caaff6afaf5a6af8fa518e
9 files changed
+28
-27
builtin-log.c
+1
-1
@@ -112,7 +112,7 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
112
int len = 0;
113
114
if (output_directory) {
115
- strncpy(filename, output_directory, 1010);
115
+ safe_strncpy(filename, output_directory, 1010);
116
len = strlen(filename);
117
if (filename[len - 1] != '/')
118
filename[len++] = '/';
builtin-tar-tree.c
+2
-2
@@ -240,8 +240,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
240
/* XXX: should we provide more meaningful info here? */
241
sprintf(header.uid, "%07o", 0);
242
sprintf(header.gid, "%07o", 0);
243
- strncpy(header.uname, "git", 31);
244
- strncpy(header.gname, "git", 31);
243
+ safe_strncpy(header.uname, "git", sizeof(header.uname));
244
+ safe_strncpy(header.gname, "git", sizeof(header.gname));
245
sprintf(header.devmajor, "%07o", 0);
246
sprintf(header.devminor, "%07o", 0);
247
cache.h
+1
-1
@@ -210,7 +210,7 @@ int git_mkstemp(char *path, size_t n, const char *template);
210
211
int adjust_shared_perm(const char *path);
212
int safe_create_leading_directories(char *path);
213
-char *safe_strncpy(char *, const char *, size_t);
213
+size_t safe_strncpy(char *, const char *, size_t);
214
char *enter_repo(char *path, int strict);
215
216
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
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
- strncpy(git_default_name, value, sizeof(git_default_name));
283
+ safe_strncpy(git_default_name, value, sizeof(git_default_name));
284
return 0;
285
}
286
287
if (!strcmp(var, "user.email")) {
288
- strncpy(git_default_email, value, sizeof(git_default_email));
288
+ safe_strncpy(git_default_email, value, sizeof(git_default_email));
289
return 0;
290
}
291
292
if (!strcmp(var, "i18n.commitencoding")) {
293
- strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
293
+ safe_strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
294
return 0;
295
}
296
http-fetch.c
+4
-6
@@ -584,10 +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
- strncpy(target, base, serverlen);
588
- strncpy(target + serverlen, data + i,
589
- posn - i - 7);
590
- target[serverlen + posn - i - 7] = '\0';
587
+ safe_strncpy(target, base, serverlen);
588
+ safe_strncpy(target + serverlen, data + i, posn - i - 6);
589
if (get_verbosely)
590
fprintf(stderr,
591
"Also look at %s\n", target);
@@ -728,8 +726,8 @@ xml_cdata(void *userData, const XML_Char *s, int len)
726
struct xml_ctx *ctx = (struct xml_ctx *)userData;
727
if (ctx->cdata)
728
free(ctx->cdata);
731
- ctx->cdata = xcalloc(len+1, 1);
732
- strncpy(ctx->cdata, s, len);
729
+ ctx->cdata = xmalloc(len + 1);
730
+ safe_strncpy(ctx->cdata, s, len + 1);
731
}
732
733
static int remote_ls(struct alt_base *repo, const char *path, int flags,
http-push.c
+5
-5
@@ -1269,8 +1269,8 @@ xml_cdata(void *userData, const XML_Char *s, int len)
1269
struct xml_ctx *ctx = (struct xml_ctx *)userData;
1270
if (ctx->cdata)
1271
free(ctx->cdata);
1272
- ctx->cdata = xcalloc(len+1, 1);
1273
- strncpy(ctx->cdata, s, len);
1272
+ ctx->cdata = xmalloc(len + 1);
1273
+ safe_strncpy(ctx->cdata, s, len + 1);
1274
}
1275
1276
static struct remote_lock *lock_remote(char *path, long timeout)
@@ -1472,7 +1472,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
1472
return;
1473
path += 8;
1474
obj_hex = xmalloc(strlen(path));
1475
- strncpy(obj_hex, path, 2);
1475
+ safe_strncpy(obj_hex, path, 3);
1476
strcpy(obj_hex + 2, path + 3);
1477
one_remote_object(obj_hex);
1478
free(obj_hex);
@@ -2160,8 +2160,8 @@ static void fetch_symref(char *path, char **symref, unsigned char *sha1)
2160
2161
/* If it's a symref, set the refname; otherwise try for a sha1 */
2162
if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
2163
- *symref = xcalloc(buffer.posn - 5, 1);
2164
- strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 6);
2163
+ *symref = xmalloc(buffer.posn - 5);
2164
+ safe_strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
2165
} else {
2166
get_sha1_hex(buffer.buffer, sha1);
2167
}
ident.c
+2
-3
@@ -71,10 +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
- strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
74
+ safe_strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
75
else
76
- strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
77
- git_default_email[sizeof(git_default_email) - 1] = 0;
76
+ safe_strncpy(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
+9
-4
@@ -83,14 +83,19 @@ int git_mkstemp(char *path, size_t len, const char *template)
83
}
84
85
86
-char *safe_strncpy(char *dest, const char *src, size_t n)
86
+size_t safe_strncpy(char *dest, const char *src, size_t size)
87
{
88
- strncpy(dest, src, n);
89
- dest[n - 1] = '\0';
88
+ size_t ret = strlen(src);
89
91
- return dest;
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
+
99
int validate_symref(const char *path)
100
{
101
struct stat st;
sha1_name.c
+1
-2
@@ -262,8 +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
- strncpy(date_spec, str + am + 2, date_len);
266
- date_spec[date_len] = 0;
265
+ safe_strncpy(date_spec, str + am + 2, date_len + 1);
266
at_time = approxidate(date_spec);
267
free(date_spec);
268
len = am;