Merge branch 'tg/memfixes'
authorJunio C Hamano <gitster@pobox.com>
Sat, 7 Oct 2017 07:27:54 +0000 (16:27 +0900)
committerJunio C Hamano <gitster@pobox.com>
Sat, 7 Oct 2017 07:27:54 +0000 (16:27 +0900)
Fixes for a handful memory access issues identified by valgrind.

* tg/memfixes:
sub-process: use child_process.args instead of child_process.argv
http-push: fix construction of hex value from path
path.c: fix uninitialized memory access

1  2 
http-push.c
path.c
sub-process.c
diff --combined http-push.c
index d860c477c60e7baec79220d9f06330a139d0e035,df969609beed88439845d9324b2fed15ff32b109..493ee7d719d488f25cd6c45bb4c7ce1d5a75977b
@@@ -11,7 -11,6 +11,7 @@@
  #include "list-objects.h"
  #include "sigchain.h"
  #include "argv-array.h"
 +#include "packfile.h"
  
  #ifdef EXPAT_NEEDS_XMLPARSE_H
  #include <xmlparse.h>
@@@ -1018,7 -1017,7 +1018,7 @@@ static int get_oid_hex_from_objpath(con
        memcpy(hex, path, 2);
        path += 2;
        path++; /* skip '/' */
-       memcpy(hex, path, GIT_SHA1_HEXSZ - 2);
+       memcpy(hex + 2, path, GIT_SHA1_HEXSZ - 2);
  
        return get_oid_hex(hex, oid);
  }
@@@ -1523,7 -1522,6 +1523,7 @@@ static int remote_exists(const char *pa
                break;
        case HTTP_ERROR:
                error("unable to access '%s': %s", url, curl_errorstr);
 +              /* fallthrough */
        default:
                ret = -1;
        }
diff --combined path.c
index 00ec04e7a5f9e90b758855a16f420324ee158739,2fecf854fe6d87cfef34678f1bc9c864ea834cb4..2e09a7bce0688acccaca8b980c2c337a71885966
--- 1/path.c
--- 2/path.c
+++ b/path.c
@@@ -9,7 -9,6 +9,7 @@@
  #include "worktree.h"
  #include "submodule-config.h"
  #include "path.h"
 +#include "packfile.h"
  
  static int get_st_mode_bits(const char *path, int *mode)
  {
@@@ -34,11 -33,10 +34,10 @@@ static struct strbuf *get_pathname(void
        return sb;
  }
  
- static char *cleanup_path(char *path)
+ static const char *cleanup_path(const char *path)
  {
        /* Clean it up */
-       if (!memcmp(path, "./", 2)) {
-               path += 2;
+       if (skip_prefix(path, "./", &path)) {
                while (*path == '/')
                        path++;
        }
@@@ -47,7 -45,7 +46,7 @@@
  
  static void strbuf_cleanup_path(struct strbuf *sb)
  {
-       char *path = cleanup_path(sb->buf);
+       const char *path = cleanup_path(sb->buf);
        if (path > sb->buf)
                strbuf_remove(sb, 0, path - sb->buf);
  }
@@@ -64,7 -62,7 +63,7 @@@ char *mksnpath(char *buf, size_t n, con
                strlcpy(buf, bad_path, n);
                return buf;
        }
-       return cleanup_path(buf);
+       return (char *)cleanup_path(buf);
  }
  
  static int dir_prefix(const char *buf, const char *dir)
@@@ -637,9 -635,8 +636,9 @@@ void strbuf_git_common_path(struct strb
  int validate_headref(const char *path)
  {
        struct stat st;
 -      char *buf, buffer[256];
 -      unsigned char sha1[20];
 +      char buffer[256];
 +      const char *refname;
 +      struct object_id oid;
        int fd;
        ssize_t len;
  
        len = read_in_full(fd, buffer, sizeof(buffer)-1);
        close(fd);
  
 +      if (len < 0)
 +              return -1;
 +      buffer[len] = '\0';
 +
        /*
         * Is it a symbolic ref?
         */
 -      if (len < 4)
 -              return -1;
 -      if (!memcmp("ref:", buffer, 4)) {
 -              buf = buffer + 4;
 -              len -= 4;
 -              while (len && isspace(*buf))
 -                      buf++, len--;
 -              if (len >= 5 && !memcmp("refs/", buf, 5))
 +      if (skip_prefix(buffer, "ref:", &refname)) {
 +              while (isspace(*refname))
 +                      refname++;
 +              if (starts_with(refname, "refs/"))
                        return 0;
        }
  
        /*
         * Is this a detached HEAD?
         */
 -      if (!get_sha1_hex(buffer, sha1))
 +      if (!get_oid_hex(buffer, &oid))
                return 0;
  
        return -1;
@@@ -718,7 -715,7 +717,7 @@@ char *expand_user_path(const char *path
                        if (!home)
                                goto return_null;
                        if (real_home)
 -                              strbuf_addstr(&user_path, real_path(home));
 +                              strbuf_add_real_path(&user_path, home);
                        else
                                strbuf_addstr(&user_path, home);
  #ifdef GIT_WINDOWS_NATIVE
diff --combined sub-process.c
index 6dde5062bef388fcdd3db10d9af19f8ffc7f7d6f,648b3a3943d68eb8a6cfc975541df8df152c4a45..8d2a1707cfe1a7d0fb6b589f0857a6389a8a3d70
@@@ -6,13 -6,10 +6,13 @@@
  #include "pkt-line.h"
  
  int cmd2process_cmp(const void *unused_cmp_data,
 -                  const struct subprocess_entry *e1,
 -                  const struct subprocess_entry *e2,
 +                  const void *entry,
 +                  const void *entry_or_key,
                    const void *unused_keydata)
  {
 +      const struct subprocess_entry *e1 = entry;
 +      const struct subprocess_entry *e2 = entry_or_key;
 +
        return strcmp(e1->cmd, e2->cmd);
  }
  
@@@ -77,13 -74,12 +77,12 @@@ int subprocess_start(struct hashmap *ha
  {
        int err;
        struct child_process *process;
-       const char *argv[] = { cmd, NULL };
  
        entry->cmd = cmd;
        process = &entry->process;
  
        child_process_init(process);
-       process->argv = argv;
+       argv_array_push(&process->args, cmd);
        process->use_shell = 1;
        process->in = -1;
        process->out = -1;
@@@ -184,8 -180,8 +183,8 @@@ static int handshake_capabilities(struc
                        if (supported_capabilities)
                                *supported_capabilities |= capabilities[i].flag;
                } else {
 -                      warning("subprocess '%s' requested unsupported capability '%s'",
 -                              process->argv[0], p);
 +                      die("subprocess '%s' requested unsupported capability '%s'",
 +                          process->argv[0], p);
                }
        }