Browse Source

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>
maint
Shawn O. Pearce 18 years ago committed by Junio C Hamano
parent
commit
3a55602eec
  1. 20
      builtin-blame.c
  2. 3
      builtin-branch.c
  3. 6
      builtin-for-each-ref.c
  4. 6
      builtin-mailinfo.c
  5. 4
      builtin-shortlog.c
  6. 3
      builtin-show-branch.c
  7. 2
      cache.h
  8. 21
      commit.c
  9. 2
      convert-objects.c
  10. 2
      environment.c
  11. 2
      fast-import.c
  12. 2
      index-pack.c
  13. 2
      interpolate.c
  14. 2
      interpolate.h
  15. 2
      path.c
  16. 3
      sha1_file.c

20
builtin-blame.c

@ -1244,26 +1244,26 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt) @@ -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, @@ -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;
}

3
builtin-branch.c

@ -289,12 +289,13 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev) @@ -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++) {

6
builtin-for-each-ref.c

@ -301,7 +301,7 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un @@ -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) @@ -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) @@ -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, '>');

6
builtin-mailinfo.c

@ -545,10 +545,10 @@ static int decode_b_segment(char *in, char *ot, char *ep) @@ -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)

4
builtin-shortlog.c

@ -217,13 +217,13 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list) @@ -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);

3
builtin-show-branch.c

@ -721,7 +721,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) @@ -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;


2
cache.h

@ -451,7 +451,7 @@ extern char git_default_email[MAX_GITNAME]; @@ -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);

21
commit.c

@ -651,7 +651,7 @@ static char *get_header(const struct commit *commit, const char *key) @@ -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) @@ -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, @@ -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);

2
convert-objects.c

@ -132,7 +132,7 @@ static void convert_tree(void *buffer, unsigned long size, unsigned char *result @@ -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);


2
environment.c

@ -21,7 +21,7 @@ int log_all_ref_updates = -1; /* unspecified */ @@ -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;

2
fast-import.c

@ -757,7 +757,7 @@ static char *create_index(void) @@ -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);

2
index-pack.c

@ -753,7 +753,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name, @@ -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;


2
interpolate.c

@ -55,7 +55,7 @@ int interpolate(char *result, int reslen, @@ -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;

2
interpolate.h

@ -12,7 +12,7 @@ @@ -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) @@ -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;
}

3
sha1_file.c

@ -2065,10 +2065,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, @@ -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);

Loading…
Cancel
Save