Merge branch 'maint-1.6.0' into maint-1.6.1
authorJunio C Hamano <gitster@pobox.com>
Sat, 18 Apr 2009 04:06:11 +0000 (21:06 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sat, 18 Apr 2009 04:06:11 +0000 (21:06 -0700)
* maint-1.6.0:
Fix buffer overflow in config parser

1  2 
config.c
diff --combined config.c
index 790405a213b12a4d1c62d9354e1292e0dc6af057,b9b2ce82378408cfd7d72245eff44efb7a8e1ec8..37e3c74861cb335d4bacab86c2f5d03cd95f43bf
+++ b/config.c
@@@ -51,7 -51,7 +51,7 @@@ static char *parse_value(void
  
        for (;;) {
                int c = get_next_char();
-               if (len >= sizeof(value))
+               if (len >= sizeof(value) - 1)
                        return NULL;
                if (c == '\n') {
                        if (quote)
@@@ -205,27 -205,8 +205,27 @@@ static int git_parse_file(config_fn_t f
        int baselen = 0;
        static char var[MAXNAME];
  
 +      /* U+FEFF Byte Order Mark in UTF8 */
 +      static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
 +      const unsigned char *bomptr = utf8_bom;
 +
        for (;;) {
                int c = get_next_char();
 +              if (bomptr && *bomptr) {
 +                      /* We are at the file beginning; skip UTF8-encoded BOM
 +                       * if present. Sane editors won't put this in on their
 +                       * own, but e.g. Windows Notepad will do it happily. */
 +                      if ((unsigned char) c == *bomptr) {
 +                              bomptr++;
 +                              continue;
 +                      } else {
 +                              /* Do not tolerate partial BOM. */
 +                              if (bomptr != utf8_bom)
 +                                      break;
 +                              /* No BOM at file beginning. Cool. */
 +                              bomptr = NULL;
 +                      }
 +              }
                if (c == '\n') {
                        if (config_file_eof)
                                return 0;
@@@ -274,7 -255,7 +274,7 @@@ static int parse_unit_factor(const cha
        return 0;
  }
  
 -int git_parse_long(const char *value, long *ret)
 +static int git_parse_long(const char *value, long *ret)
  {
        if (value && *value) {
                char *end;
@@@ -310,7 -291,7 +310,7 @@@ static void die_bad_config(const char *
  
  int git_config_int(const char *name, const char *value)
  {
 -      long ret;
 +      long ret = 0;
        if (!git_parse_long(value, &ret))
                die_bad_config(name);
        return ret;
@@@ -490,11 -471,6 +490,11 @@@ static int git_default_core_config(cons
                return 0;
        }
  
 +      if (!strcmp(var, "core.preloadindex")) {
 +              core_preload_index = git_config_bool(var, value);
 +              return 0;
 +      }
 +
        /* Add other config variables here and to Documentation/config.txt. */
        return 0;
  }
@@@ -636,7 -612,10 +636,7 @@@ int git_config(config_fn_t fn, void *da
        char *repo_config = NULL;
        const char *home = NULL;
  
 -      /* $GIT_CONFIG makes git read _only_ the given config file,
 -       * $GIT_CONFIG_LOCAL will make it process it in addition to the
 -       * global config file, the same way it would the per-repository
 -       * config file otherwise. */
 +      /* Setting $GIT_CONFIG makes git read _only_ the given config file. */
        if (config_exclusive_filename)
                return git_config_from_file(fn, config_exclusive_filename, data);
        if (git_config_system() && !access(git_etc_gitconfig(), R_OK))
@@@ -755,8 -734,9 +755,8 @@@ static int store_write_section(int fd, 
  {
        const char *dot;
        int i, success;
 -      struct strbuf sb;
 +      struct strbuf sb = STRBUF_INIT;
  
 -      strbuf_init(&sb, 0);
        dot = memchr(key, '.', store.baselen);
        if (dot) {
                strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
@@@ -781,7 -761,7 +781,7 @@@ static int store_write_pair(int fd, con
        int i, success;
        int length = strlen(key + store.baselen + 1);
        const char *quote = "";
 -      struct strbuf sb;
 +      struct strbuf sb = STRBUF_INIT;
  
        /*
         * Check to see if the value needs to be surrounded with a dq pair.
        if (i && value[i - 1] == ' ')
                quote = "\"";
  
 -      strbuf_init(&sb, 0);
        strbuf_addf(&sb, "\t%.*s = %s",
                    length, key + store.baselen + 1, quote);