General const correctness fixes

We shouldn't attempt to assign constant strings into char*, as the
string is not writable at runtime.  Likewise we should always be
treating unsigned values as unsigned values, not as signed values.

Most of these are very straightforward.  The only exception is the
(unnecessary) xstrdup/free in builtin-branch.c for the detached
head case.  Since this is a user-level interactive type program
and that particular code path is executed no more than once, I feel
that the extra xstrdup call is well worth the easy elimination of
this warning.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Shawn O. Pearce 2007-03-06 20:44:17 -05:00 коммит произвёл Junio C Hamano
Родитель ff1f99453f
Коммит 3a55602eec
16 изменённых файлов: 40 добавлений и 42 удалений

Просмотреть файл

@ -1244,26 +1244,26 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
*/
struct commit_info
{
char *author;
char *author_mail;
const char *author;
const char *author_mail;
unsigned long author_time;
char *author_tz;
const char *author_tz;
/* filled only when asked for details */
char *committer;
char *committer_mail;
const char *committer;
const char *committer_mail;
unsigned long committer_time;
char *committer_tz;
const char *committer_tz;
char *summary;
const char *summary;
};
/*
* Parse author/committer line in the commit object buffer
*/
static void get_ac_line(const char *inbuf, const char *what,
int bufsz, char *person, char **mail,
unsigned long *time, char **tz)
int bufsz, char *person, const char **mail,
unsigned long *time, const char **tz)
{
int len;
char *tmp, *endp;
@ -1280,7 +1280,7 @@ static void get_ac_line(const char *inbuf, const char *what,
if (bufsz <= len) {
error_out:
/* Ugh */
person = *mail = *tz = "(unknown)";
*mail = *tz = "(unknown)";
*time = 0;
return;
}

Просмотреть файл

@ -289,12 +289,13 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
detached = (detached && (kinds & REF_LOCAL_BRANCH));
if (detached) {
struct ref_item item;
item.name = "(no branch)";
item.name = xstrdup("(no branch)");
item.kind = REF_LOCAL_BRANCH;
hashcpy(item.sha1, head_sha1);
if (strlen(item.name) > ref_list.maxwidth)
ref_list.maxwidth = strlen(item.name);
print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
free(item.name);
}
for (i = 0; i < ref_list.index; i++) {

Просмотреть файл

@ -301,7 +301,7 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un
return "";
}
static char *copy_line(const char *buf)
static const char *copy_line(const char *buf)
{
const char *eol = strchr(buf, '\n');
char *line;
@ -315,7 +315,7 @@ static char *copy_line(const char *buf)
return line;
}
static char *copy_name(const char *buf)
static const char *copy_name(const char *buf)
{
const char *eol = strchr(buf, '\n');
const char *eoname = strstr(buf, " <");
@ -330,7 +330,7 @@ static char *copy_name(const char *buf)
return line;
}
static char *copy_email(const char *buf)
static const char *copy_email(const char *buf)
{
const char *email = strchr(buf, '<');
const char *eoemail = strchr(email, '>');

Просмотреть файл

@ -545,10 +545,10 @@ static int decode_b_segment(char *in, char *ot, char *ep)
return 0;
}
static void convert_to_utf8(char *line, char *charset)
static void convert_to_utf8(char *line, const char *charset)
{
static char latin_one[] = "latin1";
char *input_charset = *charset ? charset : latin_one;
static const char latin_one[] = "latin1";
const char *input_charset = *charset ? charset : latin_one;
char *out = reencode_string(line, metainfo_charset, input_charset);
if (!out)

Просмотреть файл

@ -217,13 +217,13 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
prepare_revision_walk(rev);
while ((commit = get_revision(rev)) != NULL) {
char *author = NULL, *oneline, *buffer;
const char *author = NULL, *oneline, *buffer;
int authorlen = authorlen, onelinelen;
/* get author and oneline */
for (buffer = commit->buffer; buffer && *buffer != '\0' &&
*buffer != '\n'; ) {
char *eol = strchr(buffer, '\n');
const char *eol = strchr(buffer, '\n');
if (eol == NULL)
eol = buffer + strlen(buffer);

Просмотреть файл

@ -721,7 +721,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
}
for (i = 0; i < reflog; i++) {
char *logmsg, *msg, *m;
char *logmsg, *m;
const char *msg;
unsigned long timestamp;
int tz;

Просмотреть файл

@ -451,7 +451,7 @@ extern char git_default_email[MAX_GITNAME];
extern char git_default_name[MAX_GITNAME];
extern char *git_commit_encoding;
extern char *git_log_output_encoding;
extern const char *git_log_output_encoding;
extern int copy_fd(int ifd, int ofd);
extern int read_in_full(int fd, void *buf, size_t count);

Просмотреть файл

@ -651,7 +651,7 @@ static char *get_header(const struct commit *commit, const char *key)
}
}
static char *replace_encoding_header(char *buf, char *encoding)
static char *replace_encoding_header(char *buf, const char *encoding)
{
char *encoding_header = strstr(buf, "\nencoding ");
char *end_of_encoding_header;
@ -694,29 +694,26 @@ static char *replace_encoding_header(char *buf, char *encoding)
}
static char *logmsg_reencode(const struct commit *commit,
char *output_encoding)
const char *output_encoding)
{
static const char *utf8 = "utf-8";
const char *use_encoding;
char *encoding;
char *out;
char *utf8 = "utf-8";
if (!*output_encoding)
return NULL;
encoding = get_header(commit, "encoding");
if (!encoding)
encoding = utf8;
if (!strcmp(encoding, output_encoding))
use_encoding = encoding ? encoding : utf8;
if (!strcmp(use_encoding, output_encoding))
out = strdup(commit->buffer);
else
out = reencode_string(commit->buffer,
output_encoding, encoding);
output_encoding, use_encoding);
if (out)
out = replace_encoding_header(out, output_encoding);
if (encoding != utf8)
free(encoding);
if (!out)
return NULL;
free(encoding);
return out;
}
@ -917,7 +914,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
const char *msg = commit->buffer;
int plain_non_ascii = 0;
char *reencoded;
char *encoding;
const char *encoding;
if (fmt == CMIT_FMT_USERFORMAT)
return format_commit_message(commit, msg, buf, space);

Просмотреть файл

@ -132,7 +132,7 @@ static void convert_tree(void *buffer, unsigned long size, unsigned char *result
unsigned long orig_size = size;
while (size) {
int len = 1+strlen(buffer);
size_t len = 1+strlen(buffer);
convert_binary_sha1((char *) buffer + len);

Просмотреть файл

@ -21,7 +21,7 @@ int log_all_ref_updates = -1; /* unspecified */
int warn_ambiguous_refs = 1;
int repository_format_version;
char *git_commit_encoding;
char *git_log_output_encoding;
const char *git_log_output_encoding;
int shared_repository = PERM_UMASK;
const char *apply_default_whitespace;
int zlib_compression_level = Z_DEFAULT_COMPRESSION;

Просмотреть файл

@ -757,7 +757,7 @@ static char *create_index(void)
static char *keep_pack(char *curr_index_name)
{
static char name[PATH_MAX];
static char *keep_msg = "fast-import";
static const char *keep_msg = "fast-import";
int keep_fd;
chmod(pack_data->pack_name, 0444);

Просмотреть файл

@ -753,7 +753,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
const char *keep_name, const char *keep_msg,
unsigned char *sha1)
{
char *report = "pack";
const char *report = "pack";
char name[PATH_MAX];
int err;

Просмотреть файл

@ -55,7 +55,7 @@ int interpolate(char *result, int reslen,
const char *src = orig;
char *dest = result;
int newlen = 0;
char *name, *value;
const char *name, *value;
int namelen, valuelen;
int i;
char c;

Просмотреть файл

@ -12,7 +12,7 @@
*/
struct interp {
char *name;
const char *name;
char *value;
};

2
path.c
Просмотреть файл

@ -252,7 +252,7 @@ char *enter_repo(char *path, int strict)
if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
validate_headref("HEAD") == 0) {
putenv("GIT_DIR=.");
setenv("GIT_DIR", ".", 1);
check_repository_format();
return path;
}

Просмотреть файл

@ -2065,10 +2065,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
enum object_type type, const char *path)
{
unsigned long size = st->st_size;
void *buf;
void *buf = NULL;
int ret, re_allocated = 0;
buf = "";
if (size)
buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);