1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-07 18:36:08 +02:00

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>
This commit is contained in:
Peter Eriksen 2006-06-24 16:01:25 +02:00 committed by Junio C Hamano
parent 3eaa38da94
commit 817151e61a
12 changed files with 48 additions and 30 deletions

View File

@ -26,6 +26,8 @@ all:
#
# Define NO_STRCASESTR if you don't have strcasestr.
#
# Define NO_STRLCPY if you don't have strlcpy.
#
# Define NO_SETENV if you don't have setenv in the C library.
#
# Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@ -240,9 +242,13 @@ LIBS = $(GITLIBS) -lz
# because maintaining the nesting to match is a pain. If
# we had "elif" things would have been much nicer...
ifeq ($(uname_S),Linux)
NO_STRLCPY = YesPlease
endif
ifeq ($(uname_S),Darwin)
NEEDS_SSL_WITH_CRYPTO = YesPlease
NEEDS_LIBICONV = YesPlease
NO_STRLCPY = YesPlease
## fink
ifeq ($(shell test -d /sw/lib && echo y),y)
ALL_CFLAGS += -I/sw/include
@ -259,6 +265,7 @@ ifeq ($(uname_S),SunOS)
NEEDS_NSL = YesPlease
SHELL_PATH = /bin/bash
NO_STRCASESTR = YesPlease
NO_STRLCPY = YesPlease
ifeq ($(uname_R),5.8)
NEEDS_LIBICONV = YesPlease
NO_UNSETENV = YesPlease
@ -276,6 +283,7 @@ ifeq ($(uname_O),Cygwin)
NO_D_TYPE_IN_DIRENT = YesPlease
NO_D_INO_IN_DIRENT = YesPlease
NO_STRCASESTR = YesPlease
NO_STRLCPY = YesPlease
NO_SYMLINK_HEAD = YesPlease
NEEDS_LIBICONV = YesPlease
# There are conflicting reports about this.
@ -305,12 +313,14 @@ ifeq ($(uname_S),NetBSD)
endif
ifeq ($(uname_S),AIX)
NO_STRCASESTR=YesPlease
NO_STRLCPY = YesPlease
NEEDS_LIBICONV=YesPlease
endif
ifeq ($(uname_S),IRIX64)
NO_IPV6=YesPlease
NO_SETENV=YesPlease
NO_STRCASESTR=YesPlease
NO_STRLCPY = YesPlease
NO_SOCKADDR_STORAGE=YesPlease
SHELL_PATH=/usr/gnu/bin/bash
ALL_CFLAGS += -DPATH_MAX=1024
@ -403,6 +413,10 @@ ifdef NO_STRCASESTR
COMPAT_CFLAGS += -DNO_STRCASESTR
COMPAT_OBJS += compat/strcasestr.o
endif
ifdef NO_STRLCPY
COMPAT_CFLAGS += -DNO_STRLCPY
COMPAT_OBJS += compat/strlcpy.o
endif
ifdef NO_SETENV
COMPAT_CFLAGS += -DNO_SETENV
COMPAT_OBJS += compat/setenv.o

View File

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

View File

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

View File

@ -216,7 +216,6 @@ enum sharedrepo {
int git_config_perm(const char *var, const char *value);
int adjust_shared_perm(const char *path);
int safe_create_leading_directories(char *path);
size_t safe_strncpy(char *, const char *, size_t);
char *enter_repo(char *path, int strict);
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */

13
compat/strlcpy.c Normal file
View File

@ -0,0 +1,13 @@
#include <string.h>
size_t gitstrlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);
if (size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}
return ret;
}

View File

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

View File

@ -79,6 +79,11 @@ extern void gitunsetenv(const char *);
extern char *gitstrcasestr(const char *haystack, const char *needle);
#endif
#ifdef NO_STRLCPY
#define strlcpy gitstrlcpy
extern size_t gitstrlcpy(char *, const char *, size_t);
#endif
static inline void *xmalloc(size_t size)
{
void *ret = malloc(size);

View File

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

View File

@ -1271,7 +1271,7 @@ xml_cdata(void *userData, const XML_Char *s, int len)
if (ctx->cdata)
free(ctx->cdata);
ctx->cdata = xmalloc(len + 1);
safe_strncpy(ctx->cdata, s, len + 1);
strlcpy(ctx->cdata, s, len + 1);
}
static struct remote_lock *lock_remote(char *path, long timeout)
@ -1473,7 +1473,7 @@ static void process_ls_object(struct remote_ls_ctx *ls)
return;
path += 8;
obj_hex = xmalloc(strlen(path));
safe_strncpy(obj_hex, path, 3);
strlcpy(obj_hex, path, 3);
strcpy(obj_hex + 2, path + 3);
one_remote_object(obj_hex);
free(obj_hex);
@ -2172,7 +2172,7 @@ 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 (!strncmp((char *)buffer.buffer, "ref: ", 5)) {
*symref = xmalloc(buffer.posn - 5);
safe_strncpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
strlcpy(*symref, (char *)buffer.buffer + 5, buffer.posn - 5);
} else {
get_sha1_hex(buffer.buffer, sha1);
}

View File

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

15
path.c
View File

@ -77,25 +77,12 @@ int git_mkstemp(char *path, size_t len, const char *template)
pch += n;
}
safe_strncpy(pch, template, len);
strlcpy(pch, template, len);
return mkstemp(path);
}
size_t safe_strncpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);
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)
{
struct stat st;

View File

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