{"thread":{"id":"7131","subject":"[PATCH 4/9] General const correctness fixes","startedAt":"2007-03-07T01:44:17Z","lastAt":"2007-03-07T01:44:17Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":9},"messages":[{"id":"36521","messageId":"20070307014417.GE26482@spearce.org","threadId":"7131","inReplyTo":null,"subject":"[PATCH 4/9] General const correctness fixes","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-07T01:44:17Z","receivedAt":"2007-03-07T01:44:17Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"We shouldn't attempt to assign constant strings into char*, as the\nstring is not writable at runtime.  Likewise we should always be\ntreating unsigned values as unsigned values, not as signed values.\n\nMost of these are very straightforward.  The only exception is the\n(unnecessary) xstrdup/free in builtin-branch.c for the detached\nhead case.  Since this is a user-level interactive type program\nand that particular code path is executed no more than once, I feel\nthat the extra xstrdup call is well worth the easy elimination of\nthis warning.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n builtin-blame.c        |   20 ++++++++++----------\n builtin-branch.c       |    3 ++-\n builtin-for-each-ref.c |    6 +++---\n builtin-mailinfo.c     |    6 +++---\n builtin-shortlog.c     |    4 ++--\n builtin-show-branch.c  |    3 ++-\n cache.h                |    2 +-\n commit.c               |   21 +++++++++------------\n convert-objects.c      |    2 +-\n environment.c          |    2 +-\n fast-import.c          |    2 +-\n index-pack.c           |    2 +-\n interpolate.c          |    2 +-\n interpolate.h          |    2 +-\n path.c                 |    2 +-\n sha1_file.c            |    3 +--\n 16 files changed, 40 insertions(+), 42 deletions(-)\n\ndiff --git a/builtin-blame.c b/builtin-blame.c\nindex 9f7dd4e..20966b9 100644\n--- a/builtin-blame.c\n+++ b/builtin-blame.c\n@@ -1244,26 +1244,26 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)\n  */\n struct commit_info\n {\n-\tchar *author;\n-\tchar *author_mail;\n+\tconst char *author;\n+\tconst char *author_mail;\n \tunsigned long author_time;\n-\tchar *author_tz;\n+\tconst char *author_tz;\n \n \t/* filled only when asked for details */\n-\tchar *committer;\n-\tchar *committer_mail;\n+\tconst char *committer;\n+\tconst char *committer_mail;\n \tunsigned long committer_time;\n-\tchar *committer_tz;\n+\tconst char *committer_tz;\n \n-\tchar *summary;\n+\tconst char *summary;\n };\n \n /*\n  * Parse author/committer line in the commit object buffer\n  */\n static void get_ac_line(const char *inbuf, const char *what,\n-\t\t\tint bufsz, char *person, char **mail,\n-\t\t\tunsigned long *time, char **tz)\n+\t\t\tint bufsz, char *person, const char **mail,\n+\t\t\tunsigned long *time, const char **tz)\n {\n \tint len;\n \tchar *tmp, *endp;\n@@ -1280,7 +1280,7 @@ static void get_ac_line(const char *inbuf, const char *what,\n \tif (bufsz <= len) {\n \terror_out:\n \t\t/* Ugh */\n-\t\tperson = *mail = *tz = \"(unknown)\";\n+\t\t*mail = *tz = \"(unknown)\";\n \t\t*time = 0;\n \t\treturn;\n \t}\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex d371849..06d8a8c 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -289,12 +289,13 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev)\n \tdetached = (detached && (kinds & REF_LOCAL_BRANCH));\n \tif (detached) {\n \t\tstruct ref_item item;\n-\t\titem.name = \"(no branch)\";\n+\t\titem.name = xstrdup(\"(no branch)\");\n \t\titem.kind = REF_LOCAL_BRANCH;\n \t\thashcpy(item.sha1, head_sha1);\n \t\tif (strlen(item.name) > ref_list.maxwidth)\n \t\t\t      ref_list.maxwidth = strlen(item.name);\n \t\tprint_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);\n+\t\tfree(item.name);\n \t}\n \n \tfor (i = 0; i < ref_list.index; i++) {\ndiff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c\nindex b11ca92..2b21842 100644\n--- a/builtin-for-each-ref.c\n+++ b/builtin-for-each-ref.c\n@@ -301,7 +301,7 @@ static const char *find_wholine(const char *who, int wholen, const char *buf, un\n \treturn \"\";\n }\n \n-static char *copy_line(const char *buf)\n+static const char *copy_line(const char *buf)\n {\n \tconst char *eol = strchr(buf, '\\n');\n \tchar *line;\n@@ -315,7 +315,7 @@ static char *copy_line(const char *buf)\n \treturn line;\n }\n \n-static char *copy_name(const char *buf)\n+static const char *copy_name(const char *buf)\n {\n \tconst char *eol = strchr(buf, '\\n');\n \tconst char *eoname = strstr(buf, \" <\");\n@@ -330,7 +330,7 @@ static char *copy_name(const char *buf)\n \treturn line;\n }\n \n-static char *copy_email(const char *buf)\n+static const char *copy_email(const char *buf)\n {\n \tconst char *email = strchr(buf, '<');\n \tconst char *eoemail = strchr(email, '>');\ndiff --git a/builtin-mailinfo.c b/builtin-mailinfo.c\nindex 766a37e..f54e875 100644\n--- a/builtin-mailinfo.c\n+++ b/builtin-mailinfo.c\n@@ -545,10 +545,10 @@ static int decode_b_segment(char *in, char *ot, char *ep)\n \treturn 0;\n }\n \n-static void convert_to_utf8(char *line, char *charset)\n+static void convert_to_utf8(char *line, const char *charset)\n {\n-\tstatic char latin_one[] = \"latin1\";\n-\tchar *input_charset = *charset ? charset : latin_one;\n+\tstatic const char latin_one[] = \"latin1\";\n+\tconst char *input_charset = *charset ? charset : latin_one;\n \tchar *out = reencode_string(line, metainfo_charset, input_charset);\n \n \tif (!out)\ndiff --git a/builtin-shortlog.c b/builtin-shortlog.c\nindex 2f71a2a..2d7726e 100644\n--- a/builtin-shortlog.c\n+++ b/builtin-shortlog.c\n@@ -217,13 +217,13 @@ static void get_from_rev(struct rev_info *rev, struct path_list *list)\n \n \tprepare_revision_walk(rev);\n \twhile ((commit = get_revision(rev)) != NULL) {\n-\t\tchar *author = NULL, *oneline, *buffer;\n+\t\tconst char *author = NULL, *oneline, *buffer;\n \t\tint authorlen = authorlen, onelinelen;\n \n \t\t/* get author and oneline */\n \t\tfor (buffer = commit->buffer; buffer && *buffer != '\\0' &&\n \t\t\t\t*buffer != '\\n'; ) {\n-\t\t\tchar *eol = strchr(buffer, '\\n');\n+\t\t\tconst char *eol = strchr(buffer, '\\n');\n \n \t\t\tif (eol == NULL)\n \t\t\t\teol = buffer + strlen(buffer);\ndiff --git a/builtin-show-branch.c b/builtin-show-branch.c\nindex 67ae6ba..c892f1f 100644\n--- a/builtin-show-branch.c\n+++ b/builtin-show-branch.c\n@@ -721,7 +721,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)\n \t\t}\n \n \t\tfor (i = 0; i < reflog; i++) {\n-\t\t\tchar *logmsg, *msg, *m;\n+\t\t\tchar *logmsg, *m;\n+\t\t\tconst char *msg;\n \t\t\tunsigned long timestamp;\n \t\t\tint tz;\n \ndiff --git a/cache.h b/cache.h\nindex c291163..f3d7538 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -451,7 +451,7 @@ extern char git_default_email[MAX_GITNAME];\n extern char git_default_name[MAX_GITNAME];\n \n extern char *git_commit_encoding;\n-extern char *git_log_output_encoding;\n+extern const char *git_log_output_encoding;\n \n extern int copy_fd(int ifd, int ofd);\n extern int read_in_full(int fd, void *buf, size_t count);\ndiff --git a/commit.c b/commit.c\nindex 5552527..5b9234e 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -651,7 +651,7 @@ static char *get_header(const struct commit *commit, const char *key)\n \t}\n }\n \n-static char *replace_encoding_header(char *buf, char *encoding)\n+static char *replace_encoding_header(char *buf, const char *encoding)\n {\n \tchar *encoding_header = strstr(buf, \"\\nencoding \");\n \tchar *end_of_encoding_header;\n@@ -694,29 +694,26 @@ static char *replace_encoding_header(char *buf, char *encoding)\n }\n \n static char *logmsg_reencode(const struct commit *commit,\n-\t\t\t     char *output_encoding)\n+\t\t\t     const char *output_encoding)\n {\n+\tstatic const char *utf8 = \"utf-8\";\n+\tconst char *use_encoding;\n \tchar *encoding;\n \tchar *out;\n-\tchar *utf8 = \"utf-8\";\n \n \tif (!*output_encoding)\n \t\treturn NULL;\n \tencoding = get_header(commit, \"encoding\");\n-\tif (!encoding)\n-\t\tencoding = utf8;\n-\tif (!strcmp(encoding, output_encoding))\n+\tuse_encoding = encoding ? encoding : utf8;\n+\tif (!strcmp(use_encoding, output_encoding))\n \t\tout = strdup(commit->buffer);\n \telse\n \t\tout = reencode_string(commit->buffer,\n-\t\t\t\t      output_encoding, encoding);\n+\t\t\t\t      output_encoding, use_encoding);\n \tif (out)\n \t\tout = replace_encoding_header(out, output_encoding);\n \n-\tif (encoding != utf8)\n-\t\tfree(encoding);\n-\tif (!out)\n-\t\treturn NULL;\n+\tfree(encoding);\n \treturn out;\n }\n \n@@ -917,7 +914,7 @@ unsigned long pretty_print_commit(enum cmit_fmt fmt,\n \tconst char *msg = commit->buffer;\n \tint plain_non_ascii = 0;\n \tchar *reencoded;\n-\tchar *encoding;\n+\tconst char *encoding;\n \n \tif (fmt == CMIT_FMT_USERFORMAT)\n \t\treturn format_commit_message(commit, msg, buf, space);\ndiff --git a/convert-objects.c b/convert-objects.c\nindex b5f41ae..4809f91 100644\n--- a/convert-objects.c\n+++ b/convert-objects.c\n@@ -132,7 +132,7 @@ static void convert_tree(void *buffer, unsigned long size, unsigned char *result\n \tunsigned long orig_size = size;\n \n \twhile (size) {\n-\t\tint len = 1+strlen(buffer);\n+\t\tsize_t len = 1+strlen(buffer);\n \n \t\tconvert_binary_sha1((char *) buffer + len);\n \ndiff --git a/environment.c b/environment.c\nindex 49486dd..0151ad0 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -21,7 +21,7 @@ int log_all_ref_updates = -1; /* unspecified */\n int warn_ambiguous_refs = 1;\n int repository_format_version;\n char *git_commit_encoding;\n-char *git_log_output_encoding;\n+const char *git_log_output_encoding;\n int shared_repository = PERM_UMASK;\n const char *apply_default_whitespace;\n int zlib_compression_level = Z_DEFAULT_COMPRESSION;\ndiff --git a/fast-import.c b/fast-import.c\nindex cc3347b..132dd9c 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -757,7 +757,7 @@ static char *create_index(void)\n static char *keep_pack(char *curr_index_name)\n {\n \tstatic char name[PATH_MAX];\n-\tstatic char *keep_msg = \"fast-import\";\n+\tstatic const char *keep_msg = \"fast-import\";\n \tint keep_fd;\n \n \tchmod(pack_data->pack_name, 0444);\ndiff --git a/index-pack.c b/index-pack.c\nindex cf81a99..b405864 100644\n--- a/index-pack.c\n+++ b/index-pack.c\n@@ -753,7 +753,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,\n \t\t  const char *keep_name, const char *keep_msg,\n \t\t  unsigned char *sha1)\n {\n-\tchar *report = \"pack\";\n+\tconst char *report = \"pack\";\n \tchar name[PATH_MAX];\n \tint err;\n \ndiff --git a/interpolate.c b/interpolate.c\nindex f992ef7..fb30694 100644\n--- a/interpolate.c\n+++ b/interpolate.c\n@@ -55,7 +55,7 @@ int interpolate(char *result, int reslen,\n \tconst char *src = orig;\n \tchar *dest = result;\n \tint newlen = 0;\n-\tchar *name, *value;\n+\tconst char *name, *value;\n \tint namelen, valuelen;\n \tint i;\n \tchar c;\ndiff --git a/interpolate.h b/interpolate.h\nindex 190a180..16a26b9 100644\n--- a/interpolate.h\n+++ b/interpolate.h\n@@ -12,7 +12,7 @@\n  */\n \n struct interp {\n-\tchar *name;\n+\tconst char *name;\n \tchar *value;\n };\n \ndiff --git a/path.c b/path.c\nindex c5d25a4..6395cf2 100644\n--- a/path.c\n+++ b/path.c\n@@ -252,7 +252,7 @@ char *enter_repo(char *path, int strict)\n \n \tif (access(\"objects\", X_OK) == 0 && access(\"refs\", X_OK) == 0 &&\n \t    validate_headref(\"HEAD\") == 0) {\n-\t\tputenv(\"GIT_DIR=.\");\n+\t\tsetenv(\"GIT_DIR\", \".\", 1);\n \t\tcheck_repository_format();\n \t\treturn path;\n \t}\ndiff --git a/sha1_file.c b/sha1_file.c\nindex cfce7ac..b17a828 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2065,10 +2065,9 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,\n \t     enum object_type type, const char *path)\n {\n \tunsigned long size = st->st_size;\n-\tvoid *buf;\n+\tvoid *buf = NULL;\n \tint ret, re_allocated = 0;\n \n-\tbuf = \"\";\n \tif (size)\n \t\tbuf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tclose(fd);\n-- \n1.5.0.3.863.gf0989\n"}]}