"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
const-ify got_gitconfig_get_* and other nitpicking
To:
gameoftrees@openbsd.org
Date:
Fri, 24 Sep 2021 23:33:32 +0200

Download raw body.

Thread
Hello,

Reading the code in libexec/ I saw a chance to "const-ify" the various
got_gitconfig_get_* functions: they take a section and tag pointer that
aren't modified AFAICS.  Through got-read-gitconfig.c they end up
operating on string literals anyway.

While there I've also changed some variables to be size_t since they
were used only in comparisons to other size_t and marked a couple of
functions as static for coherence.

Cheers,

Omar Polo


diff 5bcfe7292a247ccfdf132cb2aa2697551ea24b8f /home/op/w/got
blob - 9b99340d822a3a7c9e2470194986699e4657f604
file + lib/gitconfig.c
--- lib/gitconfig.c
+++ lib/gitconfig.c
@@ -103,7 +103,7 @@ struct got_gitconfig {
 };
 
 static __inline__ u_int8_t
-conf_hash(char *s)
+conf_hash(const char *s)
 {
 	u_int8_t hash = 0;
 
@@ -438,8 +438,8 @@ fail:
  * if that tag does not exist.
  */
 int
-got_gitconfig_get_num(struct got_gitconfig *conf, char *section, char *tag,
-    int def)
+got_gitconfig_get_num(struct got_gitconfig *conf, const char *section,
+    const char *tag, int def)
 {
 	char	*value = got_gitconfig_get_str(conf, section, tag);
 
@@ -477,7 +477,8 @@ got_gitconfig_match_num(struct got_gitconfig *conf, ch
 
 /* Return the string value denoted by TAG in section SECTION.  */
 char *
-got_gitconfig_get_str(struct got_gitconfig *conf, char *section, char *tag)
+got_gitconfig_get_str(struct got_gitconfig *conf, const char *section,
+    const char *tag)
 {
 	struct got_gitconfig_binding *cb;
 
@@ -605,7 +606,7 @@ cleanup:
 }
 
 struct got_gitconfig_list *
-got_gitconfig_get_tag_list(struct got_gitconfig *conf, char *section)
+got_gitconfig_get_tag_list(struct got_gitconfig *conf, const char *section)
 {
 	struct got_gitconfig_list *list = 0;
 	struct got_gitconfig_list_node *node = 0;
blob - 615d2690ac39f3fceb3704b2624a85e8de2ba65c
file + lib/got_lib_gitconfig.h
--- lib/got_lib_gitconfig.h
+++ lib/got_lib_gitconfig.h
@@ -43,11 +43,12 @@ const struct got_error *got_gitconfig_get_section_list
     struct got_gitconfig_list **, struct got_gitconfig *);
 struct got_gitconfig_list *got_gitconfig_get_list(struct got_gitconfig *,
     char *, char *);
-struct got_gitconfig_list *got_gitconfig_get_tag_list(struct got_gitconfig *, char *);
-int got_gitconfig_get_num(struct got_gitconfig *, char *, char *,
+struct got_gitconfig_list *got_gitconfig_get_tag_list(struct got_gitconfig *,
+    const char *);
+int got_gitconfig_get_num(struct got_gitconfig *, const char *, const char *,
     int);
-char *got_gitconfig_get_str(struct got_gitconfig *, char *,
-    char *);
+char *got_gitconfig_get_str(struct got_gitconfig *, const char *,
+    const char *);
 const struct got_error *got_gitconfig_open(struct got_gitconfig **, int);
 void got_gitconfig_close(struct got_gitconfig *);
 int      got_gitconfig_match_num(struct got_gitconfig *, char *, char *, int);
blob - 15451b4e3f2c64f4522ab8457cb5e6151ed1e1f4
file + libexec/got-fetch-pack/got-fetch-pack.c
--- libexec/got-fetch-pack/got-fetch-pack.c
+++ libexec/got-fetch-pack/got-fetch-pack.c
@@ -779,7 +779,7 @@ int
 main(int argc, char **argv)
 {
 	const struct got_error *err = NULL;
-	int fetchfd, packfd = -1, i;
+	int fetchfd, packfd = -1;
 	uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
 	struct imsgbuf ibuf;
 	struct imsg imsg;
@@ -791,7 +791,7 @@ main(int argc, char **argv)
 	struct got_imsg_fetch_have_ref href;
 	struct got_imsg_fetch_wanted_branch wbranch;
 	struct got_imsg_fetch_wanted_ref wref;
-	size_t datalen;
+	size_t datalen, i;
 #if 0
 	static int attached;
 	while (!attached)
blob - 3ce93f09be615d41528eb6af7e5f5a1bd77ce92f
file + libexec/got-read-gitconfig/got-read-gitconfig.c
--- libexec/got-read-gitconfig/got-read-gitconfig.c
+++ libexec/got-read-gitconfig/got-read-gitconfig.c
@@ -59,7 +59,7 @@ send_gitconfig_int(struct imsgbuf *ibuf, int value)
 
 static const struct got_error *
 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
-    char *section, char *tag, int def)
+    const char *section, const char *tag, int def)
 {
 	int value;
 
@@ -84,7 +84,7 @@ send_gitconfig_str(struct imsgbuf *ibuf, const char *v
 
 static const struct got_error *
 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
-    char *section, char *tag)
+    const char *section, const char *tag)
 {
 	char *value;
 
blob - 8fdda11dc81d93858240483333f00089526064e5
file + libexec/got-read-pack/got-read-pack.c
--- libexec/got-read-pack/got-read-pack.c
+++ libexec/got-read-pack/got-read-pack.c
@@ -106,7 +106,7 @@ done:
 	return err;
 }
 
-const struct got_error *
+static const struct got_error *
 open_commit(struct got_commit_object **commit, struct got_pack *pack,
     struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
     struct got_object_cache *objcache)
@@ -175,7 +175,7 @@ done:
 	return err;
 }
 
-const struct got_error *
+static const struct got_error *
 open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
     struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
     struct got_object_id *id, struct got_object_cache *objcache)
@@ -252,7 +252,7 @@ tree_request(struct imsg *imsg, struct imsgbuf *ibuf, 
 }
 
 static const struct got_error *
-receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
+receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
 {
 	const struct got_error *err;
 	struct imsg imsg;
@@ -432,7 +432,7 @@ find_entry_by_name(struct got_pathlist_head *entries, 
 	return NULL;
 }
 
-const struct got_error *
+static const struct got_error *
 tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
     struct got_pathlist_head *entries1, int *nentries1,
     struct got_pathlist_head *entries2, int *nentries2,
@@ -540,7 +540,7 @@ send_traversed_commits(struct got_object_id *commit_id
 {
 	const struct got_error *err;
 	struct ibuf *wbuf;
-	int i;
+	size_t i;
 
 	wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
 	    sizeof(struct got_imsg_traversed_commits) +
blob - 366a7752b050575027ad23ce7530ed0f5655788d
file + libexec/got-send-pack/got-send-pack.c
--- libexec/got-send-pack/got-send-pack.c
+++ libexec/got-send-pack/got-send-pack.c
@@ -218,7 +218,6 @@ send_their_ref(struct imsgbuf *ibuf, struct got_object
 static const struct got_error *
 send_ref_status(struct imsgbuf *ibuf, const char *refname, int success,
     struct got_pathlist_head *refs, struct got_pathlist_head *delete_refs)
-
 {
 	const struct got_error *err = NULL;
 	struct ibuf *wbuf;
@@ -575,7 +574,7 @@ int
 main(int argc, char **argv)
 {
 	const struct got_error *err = NULL;
-	int sendfd, i;
+	int sendfd;
 	struct imsgbuf ibuf;
 	struct imsg imsg;
 	struct got_pathlist_head refs;
@@ -583,7 +582,7 @@ main(int argc, char **argv)
 	struct got_pathlist_entry *pe;
 	struct got_imsg_send_request send_req;
 	struct got_imsg_send_ref href;
-	size_t datalen;
+	size_t datalen, i;
 #if 0
 	static int attached;
 	while (!attached)