@matheusbsbs / matheus-bagliano / commits / 3a55602eec
Fork 0

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>

Shawn O. Pearce committed Mar 6, 2007 at 20:44 UTC 3a55602eeca4ac8670e8698a7187e18b95683344
16 files changed +40 -42
builtin-blame.c
+10 -10
@@ -1244,26 +1244,26 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1244 */
1245 struct commit_info
1246 {
1247 - char *author;
1248 - char *author_mail;
1247 + const char *author;
1248 + const char *author_mail;
1249 unsigned long author_time;
1250 - char *author_tz;
1250 + const char *author_tz;
1251
1252 /* filled only when asked for details */
1253 - char *committer;
1254 - char *committer_mail;
1253 + const char *committer;
1254 + const char *committer_mail;
1255 unsigned long committer_time;
1256 - char *committer_tz;
1256 + const char *committer_tz;
1257
1258 - char *summary;
1258 + const char *summary;
1259 };
1260
1261 /*
1262 * Parse author/committer line in the commit object buffer
1263 */
1264 static void get_ac_line(const char *inbuf, const char *what,
1265 - int bufsz, char *person, char **mail,
1266 - unsigned long *time, char **tz)
1265 + int bufsz, char *person, const char **mail,
1266 + unsigned long *time, const char **tz)
1267 {
1268 int len;
1269 char *tmp, *endp;
@@ -1280,7 +1280,7 @@ static void get_ac_line(const char *inbuf, const char *what,
1280 if (bufsz <= len) {
1281 error_out:
1282 /* Ugh */
1283 - person = *mail = *tz = "(unknown)";
1283 + *mail = *tz = "(unknown)";
1284 *time = 0;
1285 return;
1286 }
builtin-branch.c
+2 -1
@@ -289,12 +289,13 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)
289 detached = (detached && (kinds & REF_LOCAL_BRANCH));
290 if (detached) {
291 struct ref_item item;
292 - item.name = "(no branch)";
292 + item.name = xstrdup("(no branch)");
293 item.kind = REF_LOCAL_BRANCH;
294 hashcpy(item.sha1, head_sha1);
295 if (strlen(item.name) > ref_list.maxwidth)
296 ref_list.maxwidth = strlen(item.name);
297 print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
298 + free(item.name);
299 }
300
301 for (i = 0; i < ref_list.index; i++) {
builtin-for-each-ref.c
+3 -3
@@ -301,7 +301,7 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un
301 return "";
302 }
303
304 -static char *copy_line(const char *buf)
304 +static const char *copy_line(const char *buf)
305 {
306 const char *eol = strchr(buf, '\n');
307 char *line;
@@ -315,7 +315,7 @@ static char *copy_line(const char *buf)
315 return line;
316 }
317
318 -static char *copy_name(const char *buf)
318 +static const char *copy_name(const char *buf)
319 {
320 const char *eol = strchr(buf, '\n');
321 const char *eoname = strstr(buf, " <");
@@ -330,7 +330,7 @@ static char *copy_name(const char *buf)
330 return line;
331 }
332
333 -static char *copy_email(const char *buf)
333 +static const char *copy_email(const char *buf)
334 {
335 const char *email = strchr(buf, '<');
336 const char *eoemail = strchr(email, '>');
builtin-mailinfo.c
+3 -3
@@ -545,10 +545,10 @@ static int decode_b_segment(char *in, char *ot, char *ep)
545 return 0;
546 }
547
548 -static void convert_to_utf8(char *line, char *charset)
548 +static void convert_to_utf8(char *line, const char *charset)
549 {
550 - static char latin_one[] = "latin1";
551 - char *input_charset = *charset ? charset : latin_one;
550 + static const char latin_one[] = "latin1";
551 + const char *input_charset = *charset ? charset : latin_one;
552 char *out = reencode_string(line, metainfo_charset, input_charset);
553
554 if (!out)
builtin-shortlog.c
+2 -2
@@ -217,13 +217,13 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)
217
218 prepare_revision_walk(rev);
219 while ((commit = get_revision(rev)) != NULL) {
220 - char *author = NULL, *oneline, *buffer;
220 + const char *author = NULL, *oneline, *buffer;
221 int authorlen = authorlen, onelinelen;
222
223 /* get author and oneline */
224 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
225 *buffer != '\n'; ) {
226 - char *eol = strchr(buffer, '\n');
226 + const char *eol = strchr(buffer, '\n');
227
228 if (eol == NULL)
229 eol = buffer + strlen(buffer);
builtin-show-branch.c
+2 -1
@@ -721,7 +721,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
721 }
722
723 for (i = 0; i < reflog; i++) {
724 - char *logmsg, *msg, *m;
724 + char *logmsg, *m;
725 + const char *msg;
726 unsigned long timestamp;
727 int tz;
728
cache.h
+1 -1
@@ -451,7 +451,7 @@ extern char git_default_email[MAX_GITNAME];
451 extern char git_default_name[MAX_GITNAME];
452
453 extern char *git_commit_encoding;
454 -extern char *git_log_output_encoding;
454 +extern const char *git_log_output_encoding;
455
456 extern int copy_fd(int ifd, int ofd);
457 extern int read_in_full(int fd, void *buf, size_t count);
commit.c
+9 -12
@@ -651,7 +651,7 @@ static char *get_header(const struct commit *commit, const char *key)
651 }
652 }
653
654 -static char *replace_encoding_header(char *buf, char *encoding)
654 +static char *replace_encoding_header(char *buf, const char *encoding)
655 {
656 char *encoding_header = strstr(buf, "\nencoding ");
657 char *end_of_encoding_header;
@@ -694,29 +694,26 @@ static char *replace_encoding_header(char *buf, char *encoding)
694 }
695
696 static char *logmsg_reencode(const struct commit *commit,
697 - char *output_encoding)
697 + const char *output_encoding)
698 {
699 + static const char *utf8 = "utf-8";
700 + const char *use_encoding;
701 char *encoding;
702 char *out;
701 - char *utf8 = "utf-8";
703
704 if (!*output_encoding)
705 return NULL;
706 encoding = get_header(commit, "encoding");
706 - if (!encoding)
707 - encoding = utf8;
708 - if (!strcmp(encoding, output_encoding))
707 + use_encoding = encoding ? encoding : utf8;
708 + if (!strcmp(use_encoding, output_encoding))
709 out = strdup(commit->buffer);
710 else
711 out = reencode_string(commit->buffer,
712 - output_encoding, encoding);
712 + output_encoding, use_encoding);
713 if (out)
714 out = replace_encoding_header(out, output_encoding);
715
716 - if (encoding != utf8)
717 - free(encoding);
718 - if (!out)
719 - return NULL;
716 + free(encoding);
717 return out;
718 }
719
@@ -917,7 +914,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,
914 const char *msg = commit->buffer;
915 int plain_non_ascii = 0;
916 char *reencoded;
920 - char *encoding;
917 + const char *encoding;
918
919 if (fmt == CMIT_FMT_USERFORMAT)
920 return format_commit_message(commit, msg, buf, space);
convert-objects.c
+1 -1
@@ -132,7 +132,7 @@ static void convert_tree(void *buffer, unsigned long size, unsigned char *result
132 unsigned long orig_size = size;
133
134 while (size) {
135 - int len = 1+strlen(buffer);
135 + size_t len = 1+strlen(buffer);
136
137 convert_binary_sha1((char *) buffer + len);
138
environment.c
+1 -1
@@ -21,7 +21,7 @@ int log_all_ref_updates = -1; /* unspecified */
21 int warn_ambiguous_refs = 1;
22 int repository_format_version;
23 char *git_commit_encoding;
24 -char *git_log_output_encoding;
24 +const char *git_log_output_encoding;
25 int shared_repository = PERM_UMASK;
26 const char *apply_default_whitespace;
27 int zlib_compression_level = Z_DEFAULT_COMPRESSION;
fast-import.c
+1 -1
@@ -757,7 +757,7 @@ static char *create_index(void)
757 static char *keep_pack(char *curr_index_name)
758 {
759 static char name[PATH_MAX];
760 - static char *keep_msg = "fast-import";
760 + static const char *keep_msg = "fast-import";
761 int keep_fd;
762
763 chmod(pack_data->pack_name, 0444);
index-pack.c
+1 -1
@@ -753,7 +753,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
753 const char *keep_name, const char *keep_msg,
754 unsigned char *sha1)
755 {
756 - char *report = "pack";
756 + const char *report = "pack";
757 char name[PATH_MAX];
758 int err;
759
interpolate.c
+1 -1
@@ -55,7 +55,7 @@ int interpolate(char *result, int reslen,
55 const char *src = orig;
56 char *dest = result;
57 int newlen = 0;
58 - char *name, *value;
58 + const char *name, *value;
59 int namelen, valuelen;
60 int i;
61 char c;
interpolate.h
+1 -1
@@ -12,7 +12,7 @@
12 */
13
14 struct interp {
15 - char *name;
15 + const char *name;
16 char *value;
17 };
18
path.c
+1 -1
@@ -252,7 +252,7 @@ char *enter_repo(char *path, int strict)
252
253 if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
254 validate_headref("HEAD") == 0) {
255 - putenv("GIT_DIR=.");
255 + setenv("GIT_DIR", ".", 1);
256 check_repository_format();
257 return path;
258 }
sha1_file.c
+1 -2
@@ -2065,10 +2065,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2065 enum object_type type, const char *path)
2066 {
2067 unsigned long size = st->st_size;
2068 - void *buf;
2068 + void *buf = NULL;
2069 int ret, re_allocated = 0;
2070
2071 - buf = "";
2071 if (size)
2072 buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2073 close(fd);