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>
This commit is contained in:
Peter Eriksen 2006-06-11 14:03:28 +02:00 committed by Junio C Hamano
parent 5996ca0836
commit bfbd0bb6ec
9 changed files with 28 additions and 27 deletions

View file

@ -112,7 +112,7 @@ static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
int len = 0; int len = 0;
if (output_directory) { if (output_directory) {
strncpy(filename, output_directory, 1010); safe_strncpy(filename, output_directory, 1010);
len = strlen(filename); len = strlen(filename);
if (filename[len - 1] != '/') if (filename[len - 1] != '/')
filename[len++] = '/'; filename[len++] = '/';

View file

@ -240,8 +240,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
/* XXX: should we provide more meaningful info here? */ /* XXX: should we provide more meaningful info here? */
sprintf(header.uid, "%07o", 0); sprintf(header.uid, "%07o", 0);
sprintf(header.gid, "%07o", 0); sprintf(header.gid, "%07o", 0);
strncpy(header.uname, "git", 31); safe_strncpy(header.uname, "git", sizeof(header.uname));
strncpy(header.gname, "git", 31); safe_strncpy(header.gname, "git", sizeof(header.gname));
sprintf(header.devmajor, "%07o", 0); sprintf(header.devmajor, "%07o", 0);
sprintf(header.devminor, "%07o", 0); sprintf(header.devminor, "%07o", 0);

View file

@ -210,7 +210,7 @@ int git_mkstemp(char *path, size_t n, const char *template);
int adjust_shared_perm(const char *path); int adjust_shared_perm(const char *path);
int safe_create_leading_directories(char *path); int safe_create_leading_directories(char *path);
char *safe_strncpy(char *, const char *, size_t); size_t safe_strncpy(char *, const char *, size_t);
char *enter_repo(char *path, int strict); char *enter_repo(char *path, int strict);
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */ /* Read and unpack a sha1 file into memory, write memory to a sha1 file */

View file

@ -280,17 +280,17 @@ int git_default_config(const char *var, const char *value)
} }
if (!strcmp(var, "user.name")) { if (!strcmp(var, "user.name")) {
strncpy(git_default_name, value, sizeof(git_default_name)); safe_strncpy(git_default_name, value, sizeof(git_default_name));
return 0; return 0;
} }
if (!strcmp(var, "user.email")) { if (!strcmp(var, "user.email")) {
strncpy(git_default_email, value, sizeof(git_default_email)); safe_strncpy(git_default_email, value, sizeof(git_default_email));
return 0; return 0;
} }
if (!strcmp(var, "i18n.commitencoding")) { if (!strcmp(var, "i18n.commitencoding")) {
strncpy(git_commit_encoding, value, sizeof(git_commit_encoding)); safe_strncpy(git_commit_encoding, value, sizeof(git_commit_encoding));
return 0; return 0;
} }

View file

@ -584,10 +584,8 @@ static void process_alternates_response(void *callback_data)
// skip 'objects' at end // skip 'objects' at end
if (okay) { if (okay) {
target = xmalloc(serverlen + posn - i - 6); target = xmalloc(serverlen + posn - i - 6);
strncpy(target, base, serverlen); safe_strncpy(target, base, serverlen);
strncpy(target + serverlen, data + i, safe_strncpy(target + serverlen, data + i, posn - i - 6);
posn - i - 7);
target[serverlen + posn - i - 7] = '\0';
if (get_verbosely) if (get_verbosely)
fprintf(stderr, fprintf(stderr,
"Also look at %s\n", target); "Also look at %s\n", target);
@ -728,8 +726,8 @@ xml_cdata(void *userData, const XML_Char *s, int len)
struct xml_ctx *ctx = (struct xml_ctx *)userData; struct xml_ctx *ctx = (struct xml_ctx *)userData;
if (ctx->cdata) if (ctx->cdata)
free(ctx->cdata); free(ctx->cdata);
ctx->cdata = xcalloc(len+1, 1); ctx->cdata = xmalloc(len + 1);
strncpy(ctx->cdata, s, len); safe_strncpy(ctx->cdata, s, len + 1);
} }
static int remote_ls(struct alt_base *repo, const char *path, int flags, static int remote_ls(struct alt_base *repo, const char *path, int flags,

View file

@ -1269,8 +1269,8 @@ xml_cdata(void *userData, const XML_Char *s, int len)
struct xml_ctx *ctx = (struct xml_ctx *)userData; struct xml_ctx *ctx = (struct xml_ctx *)userData;
if (ctx->cdata) if (ctx->cdata)
free(ctx->cdata); free(ctx->cdata);
ctx->cdata = xcalloc(len+1, 1); ctx->cdata = xmalloc(len + 1);
strncpy(ctx->cdata, s, len); safe_strncpy(ctx->cdata, s, len + 1);
} }
static struct remote_lock *lock_remote(char *path, long timeout) static struct remote_lock *lock_remote(char *path, long timeout)
@ -1472,7 +1472,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
return; return;
path += 8; path += 8;
obj_hex = xmalloc(strlen(path)); obj_hex = xmalloc(strlen(path));
strncpy(obj_hex, path, 2); safe_strncpy(obj_hex, path, 3);
strcpy(obj_hex + 2, path + 3); strcpy(obj_hex + 2, path + 3);
one_remote_object(obj_hex); one_remote_object(obj_hex);
free(obj_hex); free(obj_hex);
@ -2160,8 +2160,8 @@ static void fetch_symref(char *path, char **symref, unsigned char *sha1)
/* If it's a symref, set the refname; otherwise try for a sha1 */ /* If it's a symref, set the refname; otherwise try for a sha1 */
if (!strncmp((char *)buffer.buffer, "ref: ", 5)) { if (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
*symref = xcalloc(buffer.posn - 5, 1); *symref = xmalloc(buffer.posn - 5);
strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 6); safe_strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
} else { } else {
get_sha1_hex(buffer.buffer, sha1); get_sha1_hex(buffer.buffer, sha1);
} }

View file

@ -71,10 +71,9 @@ int setup_ident(void)
len = strlen(git_default_email); len = strlen(git_default_email);
git_default_email[len++] = '.'; git_default_email[len++] = '.';
if (he && (domainname = strchr(he->h_name, '.'))) if (he && (domainname = strchr(he->h_name, '.')))
strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len); safe_strncpy(git_default_email + len, domainname + 1, sizeof(git_default_email) - len);
else else
strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len); safe_strncpy(git_default_email + len, "(none)", sizeof(git_default_email) - len);
git_default_email[sizeof(git_default_email) - 1] = 0;
} }
/* And set the default date */ /* And set the default date */
datestamp(git_default_date, sizeof(git_default_date)); datestamp(git_default_date, sizeof(git_default_date));

13
path.c
View file

@ -83,13 +83,18 @@ int git_mkstemp(char *path, size_t len, const char *template)
} }
char *safe_strncpy(char *dest, const char *src, size_t n) size_t safe_strncpy(char *dest, const char *src, size_t size)
{ {
strncpy(dest, src, n); size_t ret = strlen(src);
dest[n - 1] = '\0';
return dest; if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
} }
return ret;
}
int validate_symref(const char *path) int validate_symref(const char *path)
{ {

View file

@ -262,8 +262,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') { if (str[am] == '@' && str[am+1] == '{' && str[len-1] == '}') {
int date_len = len - am - 3; int date_len = len - am - 3;
char *date_spec = xmalloc(date_len + 1); char *date_spec = xmalloc(date_len + 1);
strncpy(date_spec, str + am + 2, date_len); safe_strncpy(date_spec, str + am + 2, date_len + 1);
date_spec[date_len] = 0;
at_time = approxidate(date_spec); at_time = approxidate(date_spec);
free(date_spec); free(date_spec);
len = am; len = am;