Skip to content

Commit

Permalink
General const correctness fixes
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
spearce authored and Junio C Hamano committed Mar 7, 2007
1 parent ff1f994 commit 3a55602
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 42 deletions.
20 changes: 10 additions & 10 deletions builtin-blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion builtin-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
6 changes: 3 additions & 3 deletions builtin-for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, " <");
Expand All @@ -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, '>');
Expand Down
6 changes: 3 additions & 3 deletions builtin-mailinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions builtin-shortlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion builtin-show-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 9 additions & 12 deletions commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion convert-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion interpolate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion interpolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

struct interp {
char *name;
const char *name;
char *value;
};

Expand Down
2 changes: 1 addition & 1 deletion path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions sha1_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3a55602

Please sign in to comment.