Re: [PATCH v1 1/2] submodule: port submodule subcommand 'sync' from shell to C
From: Junio C Hamano <hidden>
Date: 2018-01-09 20:59:17
Prathamesh Chavan [off-list ref] writes:
+static int print_default_remote(int argc, const char **argv, const char *prefix)
+{
+ const char *remote;
+
+ if (argc != 1)
+ die(_("submodule--helper print-default-remote takes no arguments"));
+
+ remote = get_default_remote();
+ if (remote)
+ printf("%s\n", remote);
+
+ return 0;
+}This is called directly from main and return immediately after printing, so a small leak of remote does not matter, I guess.
+static void sync_submodule(const char *path, const char *prefix,
+ unsigned int flags)
+{
+ const struct submodule *sub;
+ char *remote_key = NULL;
+ char *sub_origin_url, *super_config_url, *displaypath;
+ struct strbuf sb = STRBUF_INIT;
+ struct child_process cp = CHILD_PROCESS_INIT;
+ char *sub_config_path = NULL;
+
+ if (!is_submodule_active(the_repository, path))
+ return;
+
+ sub = submodule_from_path(&null_oid, path);
+
+ if (sub && sub->url) {
+ if (starts_with_dot_dot_slash(sub->url) || starts_with_dot_slash(sub->url)) {
Not a big deal, but other codepaths seem to fold this pattern into
two lines, i.e.
if (starts_with_dot_dot_slash(sub->url) ||
starts_with_dot_slash(sub->url)) {
+ sub_origin_url = relative_url(remote_url, sub->url, up_path); + super_config_url = relative_url(remote_url, sub->url, NULL);
On this side, these two are allocated memory that need to be freed.
+ } else {
+ sub_origin_url = xstrdup(sub->url);
+ super_config_url = xstrdup(sub->url);This side as well.
+ }
+ } else {
+ sub_origin_url = "";
+ super_config_url = "";But not these. You have free() of these two at the end of this function, which will break things.