]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'js/mingw-needs-hiding-fix'
authorJunio C Hamano <gitster@pobox.com>
Wed, 30 Oct 2019 06:13:13 +0000 (15:13 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 30 Oct 2019 06:13:13 +0000 (15:13 +0900)
Fix for a (rather old) buffer-overrun bug.

* js/mingw-needs-hiding-fix:
  mingw: avoid a buffer overrun in `needs_hiding()`

1  2 
compat/mingw.c

diff --combined compat/mingw.c
index 6b765d936cfc8afe198b2735dafd4116c1706b58,f76ba3d750c806711c2291fabf2d81e18b8ca259..fe609239dd6ba22a843b243647868f887014d40e
@@@ -363,6 -363,8 +363,8 @@@ static inline int needs_hiding(const ch
                        /* ignore trailing slashes */
                        if (*path)
                                basename = path;
+                       else
+                               break;
                }
  
        if (hide_dotfiles == HIDE_DOTFILES_TRUE)
@@@ -1161,21 -1163,14 +1163,21 @@@ static char *lookup_prog(const char *di
                         int isexe, int exe_only)
  {
        char path[MAX_PATH];
 +      wchar_t wpath[MAX_PATH];
        snprintf(path, sizeof(path), "%.*s\\%s.exe", dirlen, dir, cmd);
  
 -      if (!isexe && access(path, F_OK) == 0)
 +      if (xutftowcs_path(wpath, path) < 0)
 +              return NULL;
 +
 +      if (!isexe && _waccess(wpath, F_OK) == 0)
                return xstrdup(path);
 -      path[strlen(path)-4] = '\0';
 -      if ((!exe_only || isexe) && access(path, F_OK) == 0)
 -              if (!(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY))
 +      wpath[wcslen(wpath)-4] = '\0';
 +      if ((!exe_only || isexe) && _waccess(wpath, F_OK) == 0) {
 +              if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) {
 +                      path[strlen(path)-4] = '\0';
                        return xstrdup(path);
 +              }
 +      }
        return NULL;
  }
  
@@@ -1236,6 -1231,11 +1238,6 @@@ static int wenvcmp(const void *a, cons
        return _wcsnicmp(p, q, p_len);
  }
  
 -/* We need a stable sort to convert the environment between UTF-16 <-> UTF-8 */
 -#ifndef INTERNAL_QSORT
 -#include "qsort.c"
 -#endif
 -
  /*
   * Build an environment block combining the inherited environment
   * merged with the given list of settings.
@@@ -1267,15 -1267,15 +1269,15 @@@ static wchar_t *make_environment_block(
                }
  
                ALLOC_ARRAY(result, size);
 -              memcpy(result, wenv, size * sizeof(*wenv));
 +              COPY_ARRAY(result, wenv, size);
                FreeEnvironmentStringsW(wenv);
                return result;
        }
  
        /*
         * If there is a deltaenv, let's accumulate all keys into `array`,
 -       * sort them using the stable git_qsort() and then copy, skipping
 -       * duplicate keys
 +       * sort them using the stable git_stable_qsort() and then copy,
 +       * skipping duplicate keys
         */
        for (p = wenv; p && *p; ) {
                ALLOC_GROW(array, nr + 1, alloc);
                p += wlen + 1;
        }
  
 -      git_qsort(array, nr, sizeof(*array), wenvcmp);
 +      git_stable_qsort(array, nr, sizeof(*array), wenvcmp);
        ALLOC_ARRAY(result, size + delta_size);
  
        for (p = result, i = 0; i < nr; i++) {
                        continue;
  
                size = wcslen(array[i]) + 1;
 -              memcpy(p, array[i], size * sizeof(*p));
 +              COPY_ARRAY(p, array[i], size);
                p += size;
        }
        *p = L'\0';
@@@ -1665,8 -1665,6 +1667,8 @@@ char *mingw_getenv(const char *name
        if (!w_key)
                die("Out of memory, (tried to allocate %u wchar_t's)", len_key);
        xutftowcs(w_key, name, len_key);
 +      /* GetEnvironmentVariableW() only sets the last error upon failure */
 +      SetLastError(ERROR_SUCCESS);
        len_value = GetEnvironmentVariableW(w_key, w_value, ARRAY_SIZE(w_value));
        if (!len_value && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
                free(w_key);