Commit 58e31a80da4f4a9e10eaea148672190f35107a47

Omar Polo 2022-06-27T18:47:02

build with -Wwrite-strings Throwing this into the mix for a while, we can always get rid of it again if it becomes annoying. No objections from stsp@

diff --git a/Makefile.inc b/Makefile.inc
index ef99224..2b72df1 100644
--- a/Makefile.inc
+++ b/Makefile.inc
@@ -11,7 +11,7 @@ LIBEXECDIR ?= ${PREFIX}/libexec
 MANDIR ?= ${PREFIX}/man/man
 .else
 CFLAGS += -Werror -Wall -Wstrict-prototypes -Wmissing-prototypes
-CFLAGS += -Wunused-variable
+CFLAGS += -Wwrite-strings -Wunused-variable
 PREFIX ?= ${HOME}
 BINDIR ?= ${PREFIX}/bin
 LIBEXECDIR ?= ${BINDIR}
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index e1fc7e7..a6bd009 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -237,7 +237,7 @@ struct gw_query_action {
 	unsigned int		 func_id;
 	const char		*func_name;
 	const struct got_error	*(*func_main)(struct gw_trans *);
-	char			*template;
+	const char		*template;
 };
 
 enum gw_query_actions {
@@ -2716,11 +2716,11 @@ gw_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
 {
 	struct tm tm;
 	time_t diff_time;
-	char *years = "years ago", *months = "months ago";
-	char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
-	char *minutes = "minutes ago", *seconds = "seconds ago";
-	char *now = "right now";
-	char *s;
+	const char *years = "years ago", *months = "months ago";
+	const char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
+	const char *minutes = "minutes ago", *seconds = "seconds ago";
+	const char *now = "right now";
+	const char *s;
 	char datebuf[29];
 
 	*repo_age = NULL;
@@ -4763,7 +4763,7 @@ static const struct got_error *
 gw_colordiff_line(struct gw_trans *gw_trans, char *buf)
 {
 	const struct got_error *error = NULL;
-	char *color = NULL;
+	const char *color = NULL;
 	enum kcgi_err kerr = KCGI_OK;
 
 	if (strncmp(buf, "-", 1) == 0)
diff --git a/include/got_diff.h b/include/got_diff.h
index 5dbe18b..4f53711 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -166,7 +166,7 @@ const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
  */
 const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
     FILE *, FILE *, struct got_object_id *, struct got_object_id *,
-    struct got_pathlist_head *, char *, char *, int, int, int,
+    struct got_pathlist_head *, const char *, const char *, int, int, int,
     struct got_repository *, FILE *);
 
 /*
diff --git a/lib/dial.c b/lib/dial.c
index 4f688a9..b7ce464 100644
--- a/lib/dial.c
+++ b/lib/dial.c
@@ -67,7 +67,7 @@ got_dial_apply_unveil(const char *proto)
 }
 
 static int
-hassuffix(char *base, char *suf)
+hassuffix(const char *base, const char *suf)
 {
 	int nb, ns;
 
@@ -207,7 +207,7 @@ got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
 	const struct got_error *error = NULL;
 	int pid, pfd[2];
 	char cmd[64];
-	char *argv[11];
+	const char *argv[11];
 	int i = 0, j;
 
 	*newpid = -1;
@@ -252,7 +252,7 @@ got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
 		n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
 		if (n < 0 || n >= ssizeof(cmd))
 			err(1, "snprintf");
-		if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
+		if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
 			err(1, "execv");
 		abort(); /* not reached */
 	} else {
diff --git a/lib/diff.c b/lib/diff.c
index 8337d25..773169c 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -62,7 +62,7 @@ diff_blobs(off_t **line_offsets, size_t *nlines,
 	const struct got_error *err = NULL, *free_err;
 	char hex1[SHA1_DIGEST_STRING_LENGTH];
 	char hex2[SHA1_DIGEST_STRING_LENGTH];
-	char *idstr1 = NULL, *idstr2 = NULL;
+	const char *idstr1 = NULL, *idstr2 = NULL;
 	off_t size1, size2;
 	struct got_diffreg_result *result;
 	off_t outoff = 0;
@@ -220,7 +220,7 @@ diff_blob_file(struct got_diffreg_result **resultp,
 {
 	const struct got_error *err = NULL, *free_err;
 	char hex1[SHA1_DIGEST_STRING_LENGTH];
-	char *idstr1 = NULL;
+	const char *idstr1 = NULL;
 	struct got_diffreg_result *result = NULL;
 
 	if (resultp)
@@ -904,9 +904,9 @@ show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
 static const struct got_error *
 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
     FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
-    struct got_pathlist_head *paths,
-    char *label1, char *label2, int diff_context, int ignore_whitespace,
-    int force_text_diff, struct got_repository *repo, FILE *outfile)
+    struct got_pathlist_head *paths, const char *label1, const char *label2,
+    int diff_context, int ignore_whitespace, int force_text_diff,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
@@ -960,9 +960,9 @@ done:
 const struct got_error *
 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
     FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
-    struct got_pathlist_head *paths,
-    char *label1, char *label2, int diff_context, int ignore_whitespace,
-    int force_text_diff, struct got_repository *repo, FILE *outfile)
+    struct got_pathlist_head *paths, const char *label1, const char *label2,
+    int diff_context, int ignore_whitespace, int force_text_diff,
+    struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	char *idstr = NULL;
diff --git a/lib/diff_output.c b/lib/diff_output.c
index 7038352..e44ac0a 100644
--- a/lib/diff_output.c
+++ b/lib/diff_output.c
@@ -262,7 +262,7 @@ diff_output_match_function_prototype(char *prototype, size_t prototype_size,
 	struct diff_atom *start_atom, *atom;
 	const struct diff_data *data;
 	unsigned char buf[DIFF_FUNCTION_CONTEXT_SIZE];
-	char *state = NULL;
+	const char *state = NULL;
 	int rc, i, ch;
 
 	if (result->left->atoms.len > 0 && cc->left.start > 0) {
diff --git a/lib/pack_create.c b/lib/pack_create.c
index 16d1a75..384b6d4 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -1829,7 +1829,7 @@ done:
 }
 
 static const struct got_error *
-hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
+hwrite(FILE *f, const void *buf, off_t len, SHA1_CTX *ctx)
 {
 	size_t n;
 
diff --git a/lib/path.c b/lib/path.c
index 19dc9c9..e3d7799 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -458,10 +458,11 @@ const struct got_error *
 got_path_find_prog(char **filename, const char *prog)
 {
 	const struct got_error *err = NULL;
+	const char *path;
 	char *p;
 	int len;
 	struct stat sbuf;
-	char *path, *pathcpy;
+	char *pathcpy, *dup = NULL;
 
 	*filename = NULL;
 
@@ -480,19 +481,22 @@ got_path_find_prog(char **filename, const char *prog)
 		return NULL;
 	}
 
-	if ((path = strdup(path)) == NULL)
+	if ((dup = strdup(path)) == NULL)
 		return got_error_from_errno("strdup");
-	pathcpy = path;
+	pathcpy = dup;
 
 	while ((p = strsep(&pathcpy, ":")) != NULL) {
-		if (*p == '\0')
-			p = ".";
+		const char *d;
 
 		len = strlen(p);
 		while (len > 0 && p[len-1] == '/')
 			p[--len] = '\0';	/* strip trailing '/' */
 
-		if (asprintf(filename, "%s/%s", p, prog) == -1) {
+		d = p;
+		if (*d == '\0')
+			d = ".";
+
+		if (asprintf(filename, "%s/%s", d, prog) == -1) {
 			err = got_error_from_errno("asprintf");
 			break;
 		}
@@ -503,7 +507,7 @@ got_path_find_prog(char **filename, const char *prog)
 		*filename = NULL;
 		continue;
 	}
-	free(path);
+	free(dup);
 	return err;
 }
 
diff --git a/lib/reference.c b/lib/reference.c
index 8616b4e..de71b58 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -998,8 +998,8 @@ got_ref_list(struct got_reflist_head *refs, struct got_repository *repo,
 {
 	const struct got_error *err;
 	char *packed_refs_path, *path_refs = NULL;
-	char *abs_namespace = NULL;
-	char *buf = NULL, *ondisk_ref_namespace = NULL;
+	char *abs_namespace = NULL, *buf = NULL;
+	const char *ondisk_ref_namespace = NULL;
 	char *line = NULL;
 	FILE *f = NULL;
 	struct got_reference *ref;
diff --git a/regress/fetch/fetch_test.c b/regress/fetch/fetch_test.c
index 9d84105..c0e7187 100644
--- a/regress/fetch/fetch_test.c
+++ b/regress/fetch/fetch_test.c
@@ -46,7 +46,7 @@ static int verbose;
 static int quiet;
 
 static void
-test_printf(char *fmt, ...)
+test_printf(const char *fmt, ...)
 {
 	va_list ap;
 
diff --git a/regress/path/path_test.c b/regress/path/path_test.c
index e8001a5..bbf71a7 100644
--- a/regress/path/path_test.c
+++ b/regress/path/path_test.c
@@ -34,7 +34,7 @@ static int verbose;
 static int quiet;
 
 static void
-test_printf(char *fmt, ...)
+test_printf(const char *fmt, ...)
 {
 	va_list ap;