1
0
mirror of https://github.com/git/git synced 2024-06-28 13:44:40 +00:00

global: improve const correctness when assigning string constants

We're about to enable `-Wwrite-strings`, which changes the type of
string constants to `const char[]`. Fix various sites where we assign
such constants to non-const variables.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt 2024-06-07 08:37:39 +02:00 committed by Junio C Hamano
parent 5529cba09f
commit b567004b4b
38 changed files with 106 additions and 102 deletions

View File

@ -262,7 +262,8 @@ static int bisect_reset(const char *commit)
return bisect_clean_state();
}
static void log_commit(FILE *fp, char *fmt, const char *state,
static void log_commit(FILE *fp,
const char *fmt, const char *state,
struct commit *commit)
{
struct pretty_print_context pp = {0};

View File

@ -134,7 +134,7 @@ static void get_ac_line(const char *inbuf, const char *what,
{
struct ident_split ident;
size_t len, maillen, namelen;
char *tmp, *endp;
const char *tmp, *endp;
const char *namebuf, *mailbuf;
tmp = strstr(inbuf, what);

View File

@ -107,7 +107,7 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
struct tm tm;
enum diagnose_mode diagnose = DIAGNOSE_NONE;
char *option_output = NULL;
char *option_suffix = "%Y-%m-%d-%H%M";
const char *option_suffix = "%Y-%m-%d-%H%M";
const char *user_relative_path = NULL;
char *prefixed_filename;
size_t output_path_len;

View File

@ -35,8 +35,8 @@ static const struct option check_ignore_options[] = {
static void output_pattern(const char *path, struct path_pattern *pattern)
{
char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : "";
const char *bang = (pattern && pattern->flags & PATTERN_FLAG_NEGATIVE) ? "!" : "";
const char *slash = (pattern && pattern->flags & PATTERN_FLAG_MUSTBEDIR) ? "/" : "";
if (!nul_term_line) {
if (!verbose) {
write_name_quoted(path, stdout, '\n');

View File

@ -71,7 +71,7 @@ static char *option_branch = NULL;
static struct string_list option_not = STRING_LIST_INIT_NODUP;
static const char *real_git_dir;
static const char *ref_format;
static char *option_upload_pack = "git-upload-pack";
static const char *option_upload_pack = "git-upload-pack";
static int option_verbosity;
static int option_progress = -1;
static int option_sparse_checkout;
@ -177,8 +177,8 @@ static struct option builtin_clone_options[] = {
static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
{
static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
static char *bundle_suffix[] = { ".bundle", "" };
static const char *suffix[] = { "/.git", "", ".git/.git", ".git" };
static const char *bundle_suffix[] = { ".bundle", "" };
size_t baselen = path->len;
struct stat st;
int i;

View File

@ -113,7 +113,7 @@ static char *template_file;
* the commit message and/or authorship.
*/
static const char *author_message, *author_message_buffer;
static char *edit_message, *use_message;
static const char *edit_message, *use_message;
static char *fixup_message, *fixup_commit, *squash_message;
static const char *fixup_prefix;
static int all, also, interactive, patch_interactive, only, amend, signoff;
@ -121,8 +121,8 @@ static int edit_flag = -1; /* unspecified */
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
static int config_commit_verbose = -1; /* unspecified */
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
static char *sign_commit, *pathspec_from_file;
static const char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
static const char *sign_commit, *pathspec_from_file;
static struct strvec trailer_args = STRVEC_INIT;
/*

View File

@ -18,7 +18,7 @@ int cmd_diagnose(int argc, const char **argv, const char *prefix)
struct tm tm;
enum diagnose_mode mode = DIAGNOSE_STATS;
char *option_output = NULL;
char *option_suffix = "%Y-%m-%d-%H%M";
const char *option_suffix = "%Y-%m-%d-%H%M";
char *prefixed_filename;
const struct option diagnose_options[] = {

View File

@ -1283,7 +1283,7 @@ static void get_patch_ids(struct rev_info *rev, struct patch_ids *ids)
o2->flags = flags2;
}
static void gen_message_id(struct rev_info *info, char *base)
static void gen_message_id(struct rev_info *info, const char *base)
{
struct strbuf buf = STRBUF_INIT;
strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,

View File

@ -113,8 +113,8 @@ static int populate_maildir_list(struct string_list *list, const char *path)
DIR *dir;
struct dirent *dent;
char *name = NULL;
char *subs[] = { "cur", "new", NULL };
char **sub;
const char *subs[] = { "cur", "new", NULL };
const char **sub;
int ret = -1;
for (sub = subs; *sub; ++sub) {

View File

@ -71,48 +71,48 @@ static const char * const pull_usage[] = {
/* Shared options */
static int opt_verbosity;
static char *opt_progress;
static const char *opt_progress;
static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
/* Options passed to git-merge or git-rebase */
static enum rebase_type opt_rebase = -1;
static char *opt_diffstat;
static char *opt_log;
static char *opt_signoff;
static char *opt_squash;
static char *opt_commit;
static char *opt_edit;
static char *cleanup_arg;
static char *opt_ff;
static char *opt_verify_signatures;
static char *opt_verify;
static const char *opt_diffstat;
static const char *opt_log;
static const char *opt_signoff;
static const char *opt_squash;
static const char *opt_commit;
static const char *opt_edit;
static const char *cleanup_arg;
static const char *opt_ff;
static const char *opt_verify_signatures;
static const char *opt_verify;
static int opt_autostash = -1;
static int config_autostash;
static int check_trust_level = 1;
static struct strvec opt_strategies = STRVEC_INIT;
static struct strvec opt_strategy_opts = STRVEC_INIT;
static char *opt_gpg_sign;
static const char *opt_gpg_sign;
static int opt_allow_unrelated_histories;
/* Options passed to git-fetch */
static char *opt_all;
static char *opt_append;
static char *opt_upload_pack;
static const char *opt_all;
static const char *opt_append;
static const char *opt_upload_pack;
static int opt_force;
static char *opt_tags;
static char *opt_prune;
static char *max_children;
static const char *opt_tags;
static const char *opt_prune;
static const char *max_children;
static int opt_dry_run;
static char *opt_keep;
static char *opt_depth;
static char *opt_unshallow;
static char *opt_update_shallow;
static char *opt_refmap;
static char *opt_ipv4;
static char *opt_ipv6;
static const char *opt_keep;
static const char *opt_depth;
static const char *opt_unshallow;
static const char *opt_update_shallow;
static const char *opt_refmap;
static const char *opt_ipv4;
static const char *opt_ipv6;
static int opt_show_forced_updates = -1;
static char *set_upstream;
static const char *set_upstream;
static struct strvec opt_fetch = STRVEC_INIT;
static struct option pull_options[] = {

View File

@ -1249,7 +1249,7 @@ static int run_proc_receive_hook(struct command *commands,
return code;
}
static char *refuse_unconfigured_deny_msg =
static const char *refuse_unconfigured_deny_msg =
N_("By default, updating the current branch in a non-bare repository\n"
"is denied, because it will make the index and work tree inconsistent\n"
"with what you pushed, and will require 'git reset --hard' to match\n"
@ -1269,7 +1269,7 @@ static void refuse_unconfigured_deny(void)
rp_error("%s", _(refuse_unconfigured_deny_msg));
}
static char *refuse_unconfigured_deny_delete_current_msg =
static const char *refuse_unconfigured_deny_delete_current_msg =
N_("By default, deleting the current branch is denied, because the next\n"
"'git clone' won't result in any file checked out, causing confusion.\n"
"\n"

View File

@ -179,7 +179,7 @@ static int run_sequencer(int argc, const char **argv, const char *prefix,
/* Check for incompatible command line arguments */
if (cmd) {
char *this_operation;
const char *this_operation;
if (cmd == 'q')
this_operation = "--quit";
else if (cmd == 'c')

View File

@ -848,7 +848,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len)
{
unsigned int table_size;
#ifndef _LIBC
char *codeset_name;
const char *codeset_name;
#endif
memset (dfa, '\0', sizeof (re_dfa_t));

4
diff.c
View File

@ -3764,7 +3764,7 @@ static void builtin_diff(const char *name_a,
return;
}
static char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
static const char *get_compact_summary(const struct diff_filepair *p, int is_renamed)
{
if (!is_renamed) {
if (p->status == DIFF_STATUS_ADDED) {
@ -4076,7 +4076,7 @@ static int reuse_worktree_file(struct index_state *istate,
static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
{
struct strbuf buf = STRBUF_INIT;
char *dirty = "";
const char *dirty = "";
/* Are we looking at the work tree? */
if (s->dirty_submodule)

View File

@ -406,7 +406,7 @@ static const char *get_highest_rename_path(struct strintmap *counts)
return highest_destination_dir;
}
static char *UNKNOWN_DIR = "/"; /* placeholder -- short, illegal directory */
static const char *UNKNOWN_DIR = "/"; /* placeholder -- short, illegal directory */
static int dir_rename_already_determinable(struct strintmap *counts)
{
@ -429,8 +429,8 @@ static int dir_rename_already_determinable(struct strintmap *counts)
}
static void increment_count(struct dir_rename_info *info,
char *old_dir,
char *new_dir)
const char *old_dir,
const char *new_dir)
{
struct strintmap *counts;
struct strmap_entry *e;

View File

@ -447,7 +447,7 @@ static void fmt_merge_msg_title(struct strbuf *out,
const char *current_branch)
{
int i = 0;
char *sep = "";
const char *sep = "";
strbuf_addstr(out, "Merge ");
for (i = 0; i < srcs.nr; i++) {

2
fsck.c
View File

@ -1231,7 +1231,7 @@ int fsck_object(struct object *obj, void *data, unsigned long size,
}
int fsck_buffer(const struct object_id *oid, enum object_type type,
void *data, unsigned long size,
const void *data, unsigned long size,
struct fsck_options *options)
{
if (type == OBJ_BLOB)

2
fsck.h
View File

@ -202,7 +202,7 @@ int fsck_object(struct object *obj, void *data, unsigned long size,
* struct.
*/
int fsck_buffer(const struct object_id *oid, enum object_type,
void *data, unsigned long size,
const void *data, unsigned long size,
struct fsck_options *options);
/*

View File

@ -727,7 +727,7 @@ static int git_gpg_config(const char *var, const char *value,
void *cb UNUSED)
{
struct gpg_format *fmt = NULL;
char *fmtname = NULL;
const char *fmtname = NULL;
char *trust;
int ret;

View File

@ -753,7 +753,7 @@ static int bad_request(struct strbuf *hdr, const struct service_cmd *c)
int cmd_main(int argc UNUSED, const char **argv UNUSED)
{
char *method = getenv("REQUEST_METHOD");
const char *method = getenv("REQUEST_METHOD");
const char *proto_header;
char *dir;
struct service_cmd *cmd = NULL;

View File

@ -1215,9 +1215,9 @@ static int imap_store_msg(struct imap_store *ctx, struct strbuf *msg)
static void wrap_in_html(struct strbuf *msg)
{
struct strbuf buf = STRBUF_INIT;
static char *content_type = "Content-Type: text/html;\n";
static char *pre_open = "<pre>\n";
static char *pre_close = "</pre>\n";
static const char *content_type = "Content-Type: text/html;\n";
static const char *pre_open = "<pre>\n";
static const char *pre_close = "</pre>\n";
const char *body = strstr(msg->buf, "\n\n");
if (!body)

View File

@ -1325,7 +1325,7 @@ int format_set_trailers_options(struct process_trailer_options *opts,
static size_t parse_describe_args(const char *start, struct strvec *args)
{
struct {
char *name;
const char *name;
enum {
DESCRIBE_ARG_BOOL,
DESCRIBE_ARG_INTEGER,

2
refs.c
View File

@ -159,7 +159,7 @@ void update_ref_namespace(enum ref_namespace namespace, char *ref)
{
struct ref_namespace_info *info = &ref_namespace[namespace];
if (info->ref_updated)
free(info->ref);
free((char *)info->ref);
info->ref = ref;
info->ref_updated = 1;
}

2
refs.h
View File

@ -975,7 +975,7 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt);
*/
struct ref_namespace_info {
char *ref;
const char *ref;
enum decoration_type decoration;
/*

View File

@ -67,9 +67,9 @@ void free_names(char **a)
reftable_free(a);
}
size_t names_length(char **names)
size_t names_length(const char **names)
{
char **p = names;
const char **p = names;
while (*p)
p++;
return p - names;
@ -102,15 +102,12 @@ void parse_names(char *buf, int size, char ***namesp)
*namesp = names;
}
int names_equal(char **a, char **b)
int names_equal(const char **a, const char **b)
{
int i = 0;
for (; a[i] && b[i]; i++) {
if (strcmp(a[i], b[i])) {
size_t i = 0;
for (; a[i] && b[i]; i++)
if (strcmp(a[i], b[i]))
return 0;
}
}
return a[i] == b[i];
}

View File

@ -42,10 +42,10 @@ void free_names(char **a);
void parse_names(char *buf, int size, char ***namesp);
/* compares two NULL-terminated arrays of strings. */
int names_equal(char **a, char **b);
int names_equal(const char **a, const char **b);
/* returns the array size of a NULL-terminated array of strings. */
size_t names_length(char **names);
size_t names_length(const char **names);
/* Allocation routines; they invoke the functions set through
* reftable_set_alloc() */

View File

@ -58,8 +58,8 @@ static void test_binsearch(void)
static void test_names_length(void)
{
char *a[] = { "a", "b", NULL };
EXPECT(names_length(a) == 2);
const char *names[] = { "a", "b", NULL };
EXPECT(names_length(names) == 2);
}
static void test_parse_names_normal(void)

View File

@ -116,7 +116,7 @@ static int decode_string(struct strbuf *dest, struct string_view in)
return start_len - in.len;
}
static int encode_string(char *str, struct string_view s)
static int encode_string(const char *str, struct string_view s)
{
struct string_view start = s;
int l = strlen(str);
@ -969,9 +969,9 @@ static int reftable_log_record_decode(void *rec, struct strbuf key,
return REFTABLE_FORMAT_ERROR;
}
static int null_streq(char *a, char *b)
static int null_streq(const char *a, const char *b)
{
char *empty = "";
const char *empty = "";
if (!a)
a = empty;

View File

@ -204,7 +204,8 @@ static struct reftable_reader **stack_copy_readers(struct reftable_stack *st,
return cur;
}
static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
static int reftable_stack_reload_once(struct reftable_stack *st,
const char **names,
int reuse_open)
{
size_t cur_len = !st->merged ? 0 : st->merged->stack_len;
@ -222,7 +223,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names,
while (*names) {
struct reftable_reader *rd = NULL;
char *name = *names++;
const char *name = *names++;
/* this is linear; we assume compaction keeps the number of
tables under control so this is not quadratic. */
@ -354,7 +355,7 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
goto out;
}
err = reftable_stack_reload_once(st, names, reuse_open);
err = reftable_stack_reload_once(st, (const char **) names, reuse_open);
if (!err)
break;
if (err != REFTABLE_NOT_EXIST_ERROR)
@ -368,7 +369,8 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
err = read_lines(st->list_file, &names_after);
if (err < 0)
goto out;
if (names_equal(names_after, names)) {
if (names_equal((const char **) names_after,
(const char **) names)) {
err = REFTABLE_NOT_EXIST_ERROR;
goto out;
}

View File

@ -83,7 +83,7 @@ static void test_read_file(void)
char out[1024] = "line1\n\nline2\nline3";
int n, err;
char **names = NULL;
char *want[] = { "line1", "line2", "line3" };
const char *want[] = { "line1", "line2", "line3" };
int i = 0;
EXPECT(fd > 0);
@ -116,9 +116,9 @@ static void test_parse_names(void)
static void test_names_equal(void)
{
char *a[] = { "a", "b", "c", NULL };
char *b[] = { "a", "b", "d", NULL };
char *c[] = { "a", "b", NULL };
const char *a[] = { "a", "b", "c", NULL };
const char *b[] = { "a", "b", "d", NULL };
const char *c[] = { "a", "b", NULL };
EXPECT(names_equal(a, a));
EXPECT(!names_equal(a, b));

View File

@ -663,7 +663,7 @@ int start_command(struct child_process *cmd)
int need_in, need_out, need_err;
int fdin[2], fdout[2], fderr[2];
int failed_errno;
char *str;
const char *str;
/*
* In case of errors we must keep the promise to close FDs

View File

@ -36,7 +36,8 @@ static int test_entry_cmp(const void *cmp_data,
}
static struct test_entry *alloc_test_entry(unsigned int hash,
char *key, char *value)
const char *key,
const char *value)
{
size_t klen = strlen(key);
size_t vlen = strlen(value);

View File

@ -174,7 +174,7 @@ static void make_arr4(int pretty)
jw_end(&arr4);
}
static char *expect_nest1 =
static const char *expect_nest1 =
"{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},\"arr1\":[\"abc\",42,true]}";
static struct json_writer nest1 = JSON_WRITER_INIT;
@ -195,10 +195,10 @@ static void make_nest1(int pretty)
jw_release(&arr1);
}
static char *expect_inline1 =
static const char *expect_inline1 =
"{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},\"arr1\":[\"abc\",42,true]}";
static char *pretty_inline1 =
static const char *pretty_inline1 =
("{\n"
" \"obj1\": {\n"
" \"a\": \"abc\",\n"
@ -236,10 +236,10 @@ static void make_inline1(int pretty)
jw_end(&inline1);
}
static char *expect_inline2 =
static const char *expect_inline2 =
"[[1,2],[3,4],{\"a\":\"abc\"}]";
static char *pretty_inline2 =
static const char *pretty_inline2 =
("[\n"
" [\n"
" 1,\n"

View File

@ -20,8 +20,8 @@ static struct reg_flag reg_flags[] = {
static int test_regex_bug(void)
{
char *pat = "[^={} \t]+";
char *str = "={}\nfred";
const char *pat = "[^={} \t]+";
const char *str = "={}\nfred";
regex_t r;
regmatch_t m[1];

View File

@ -136,7 +136,7 @@ static void free_delay_entries(void)
strmap_clear(&delay, 0);
}
static void add_delay_entry(char *pathname, int count, int requested)
static void add_delay_entry(const char *pathname, int count, int requested)
{
struct delay_entry *entry = xcalloc(1, sizeof(*entry));
entry->count = count;
@ -189,7 +189,8 @@ static void reply_list_available_blobs_cmd(void)
static void command_loop(void)
{
for (;;) {
char *buf, *output;
char *buf;
const char *output;
char *pathname;
struct delay_entry *entry;
struct strbuf input = STRBUF_INIT;

View File

@ -2,7 +2,8 @@
#include "strbuf.h"
/* wrapper that supplies tests with an empty, initialized strbuf */
static void setup(void (*f)(struct strbuf*, void*), void *data)
static void setup(void (*f)(struct strbuf*, const void*),
const void *data)
{
struct strbuf buf = STRBUF_INIT;
@ -13,7 +14,8 @@ static void setup(void (*f)(struct strbuf*, void*), void *data)
}
/* wrapper that supplies tests with a populated, initialized strbuf */
static void setup_populated(void (*f)(struct strbuf*, void*), char *init_str, void *data)
static void setup_populated(void (*f)(struct strbuf*, const void*),
const char *init_str, const void *data)
{
struct strbuf buf = STRBUF_INIT;
@ -64,7 +66,7 @@ static void t_dynamic_init(void)
strbuf_release(&buf);
}
static void t_addch(struct strbuf *buf, void *data)
static void t_addch(struct strbuf *buf, const void *data)
{
const char *p_ch = data;
const char ch = *p_ch;
@ -83,7 +85,7 @@ static void t_addch(struct strbuf *buf, void *data)
check_char(buf->buf[buf->len], ==, '\0');
}
static void t_addstr(struct strbuf *buf, void *data)
static void t_addstr(struct strbuf *buf, const void *data)
{
const char *text = data;
size_t len = strlen(text);

View File

@ -63,7 +63,7 @@ struct arg_item {
static LIST_HEAD(conf_head);
static char *separators = ":";
static const char *separators = ":";
static int configured;

View File

@ -2408,7 +2408,7 @@ static void wt_porcelain_v2_print_unmerged_entry(
int mode;
struct object_id oid;
} stages[3];
char *key;
const char *key;
char submodule_token[5];
char unmerged_prefix = 'u';
char eol_char = s->null_termination ? '\0' : '\n';