1. 06 10月, 2015 8 次提交
    • J
      remove_leading_path: use a strbuf for internal storage · 46357688
      Jeff King 提交于
      This function strcpy's directly into a PATH_MAX-sized
      buffer. There's only one caller, which feeds the git_dir into
      it, so it's not easy to trigger in practice (even if you fed
      a large $GIT_DIR through the environment or .git file, it
      would have to actually exist and be accessible on the
      filesystem to get to this point). We can fix it by moving to
      a strbuf.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      46357688
    • J
      enter_repo: convert fixed-size buffers to strbufs · e9ba6781
      Jeff King 提交于
      We use two PATH_MAX-sized buffers to represent the repo
      path, and must make sure not to overflow them. We do take
      care to check the lengths, but the logic is rather hard to
      follow, as we use several magic numbers (e.g., "PATH_MAX -
      10"). And in fact you _can_ overflow the buffer if you have
      a ".git" file with an extremely long path in it.
      
      By switching to strbufs, these problems all go away. We do,
      however, retain the check that the initial input we get is
      no larger than PATH_MAX. This function is an entry point for
      untrusted repo names from the network, and it's a good idea
      to keep a sanity check (both to avoid allocating arbitrary
      amounts of memory, and also as a layer of defense against
      any downstream users of the names).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      e9ba6781
    • J
      merge-recursive: convert malloc / strcpy to strbuf · b4600fbe
      Jeff King 提交于
      This would be a fairly routine use of xstrfmt, except that
      we need to remember the length of the result to pass to
      cache_name_pos. So just use a strbuf, which makes this
      simple.
      
      As a bonus, this gets rid of confusing references to
      "pathlen+1". The "1" is for the trailing slash we added, but
      that is automatically accounted for in the strbuf's len
      parameter.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      b4600fbe
    • J
      transport: use strbufs for status table "quickref" strings · bd22d4ff
      Jeff King 提交于
      We generate range strings like "1234abcd...5678efab" for use
      in the the fetch and push status tables. We use fixed-size
      buffers along with strcat to do so. These aren't buggy, as
      our manual size computation is correct, but there's nothing
      checking that this is so.  Let's switch them to strbufs
      instead, which are obviously correct, and make it easier to
      audit the code base for problematic calls to strcat().
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      bd22d4ff
    • J
      apply: convert root string to strbuf · 6c31c22c
      Jeff King 提交于
      We use manual computation and strcpy to allocate the "root"
      variable. This would be much simpler using xstrfmt.  But
      since we store the length, too, we can just use a strbuf,
      which handles that for us.
      
      Note that we stop distinguishing between "no root" and
      "empty root" in some cases, but that's OK; the results are
      the same (e.g., inserting an empty string is a noop).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      6c31c22c
    • J
      init: use strbufs to store paths · 9c28390b
      Jeff King 提交于
      The init code predates strbufs, and uses PATH_MAX-sized
      buffers along with many manual checks on intermediate sizes
      (some of which make magic assumptions, such as that init
      will not create a path inside .git longer than 50
      characters).
      
      We can simplify this greatly by using strbufs, which drops
      some hard-to-verify strcpy calls in favor of git_path_buf.
      While we're in the area, let's also convert existing calls
      to git_path to the safer git_path_buf (our existing calls
      were passed to pretty tame functions, and so were not a
      problem, but it's easy to be consistent and safe here).
      
      Note that we had an explicit test that "git init" rejects
      long template directories. This comes from 32d1776b (init: Do
      not segfault on big GIT_TEMPLATE_DIR environment variable,
      2009-04-18). We can drop the test_must_fail here, as we now
      accept this and need only confirm that we don't segfault,
      which was the original point of the test.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      9c28390b
    • J
      probe_utf8_pathname_composition: use internal strbuf · fdf72966
      Jeff King 提交于
      When we are initializing a .git directory, we may call
      probe_utf8_pathname_composition to detect utf8 mangling. We
      pass in a path buffer for it to use, and it blindly
      strcpy()s into it, not knowing whether the buffer is large
      enough to hold the result or not.
      
      In practice this isn't a big deal, because the buffer we
      pass in already contains "$GIT_DIR/config", and we append
      only a few extra bytes to it. But we can easily do the right
      thing just by calling git_path_buf ourselves. Technically
      this results in a different pathname (before we appended our
      utf8 characters to the "config" path, and now they get their
      own files in $GIT_DIR), but that should not matter for our
      purposes.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      fdf72966
    • J
      precompose_utf8: drop unused variable · e2b021eb
      Jeff King 提交于
      The result of iconv is assigned to a variable, but we never
      use it (instead, we check errno and whether the function
      consumed all bytes). Let's drop the assignment, as it
      triggers gcc's -Wunused-but-set-variable.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      e2b021eb
  2. 26 9月, 2015 32 次提交
    • J
      sha1_get_pack_name: use a strbuf · ac5190cc
      Jeff King 提交于
      We do some manual memory computation here, and there's no
      check that our 60 is not overflowed by the raw sprintf (it
      isn't, because the "which" parameter is never longer than
      "pack"). We can simplify this greatly with a strbuf.
      
      Technically the end result is not identical, as the original
      took care not to rewrite the object directory on each call
      for performance reasons.  We could do that here, too (by
      saving the baselen and resetting to it), but it's not worth
      the complexity; this function is not called a lot (generally
      once per packfile that we open).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ac5190cc
    • J
      http-walker: store url in a strbuf · 54ba4c5f
      Jeff King 提交于
      We do an unchecked sprintf directly into our url buffer.
      This doesn't overflow because we know that it was sized for
      "$base/objects/info/http-alternates", and we are writing
      "$base/objects/info/alternates", which must be smaller. But
      that is not immediately obvious to a reader who is looking
      for buffer overflows. Let's switch to a strbuf, so that we
      do not have to think about this issue at all.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      54ba4c5f
    • J
      http-push: use strbuf instead of fwrite_buffer · 7d0581a9
      Jeff King 提交于
      The http-push code defines an fwrite_buffer function for use
      as a curl callback; it just writes to a strbuf. There's no
      reason we need to use it ourselves, as we know we have a
      strbuf. This lets us format directly into it, rather than
      dealing with an extra temporary buffer (which required
      manual length computation).
      
      While we're here, let's also remove the literal tabs from
      the source in favor of "\t", which is more visually obvious.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      7d0581a9
    • J
      remote-ext: simplify git pkt-line generation · df1ed03a
      Jeff King 提交于
      We format a pkt-line into a heap buffer, which requires
      manual computation of the required size, and uses some bare
      sprintf calls. We could use a strbuf instead, which would
      take care of the computation for us. But it's even easier
      still to use packet_write(). Besides handling the formatting
      and writing for us, it fixes two things:
      
        1. Our manual max-size check used 0xFFFF, while technically
           LARGE_PACKET_MAX is slightly smaller than this.
      
        2. Our packet will now be output as part of
           GIT_TRACE_PACKET debugging.
      
      Unfortunately packet_write() does not let us build up the
      buffer progressively, so we do have to repeat ourselves a
      little depending on the "vhost" setting, but the end result
      is still far more readable than the original.
      
      Since there were no tests covering this feature at all,
      we'll add a few into t5802.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      df1ed03a
    • J
      upload-archive: convert sprintf to strbuf · 0cb9d6d6
      Jeff King 提交于
      When we report an error to the client, we format it into a
      fixed-size buffer using vsprintf(). This can't actually
      overflow in practice, since we only format a very tame
      subset of strings (mostly strerror() output). However, it's
      hard to tell immediately, so let's just use a strbuf so
      readers do not have to wonder.
      
      We do add an allocation here, but the performance is not
      important; the next step is to call die() anyway.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0cb9d6d6
    • J
      resolve_ref: use strbufs for internal buffers · 495127db
      Jeff King 提交于
      resolve_ref already uses a strbuf internally when generating
      pathnames, but it uses fixed-size buffers for storing the
      refname and symbolic refs. This means that you cannot
      actually point HEAD to a ref that is larger than 256 bytes.
      
      We can lift this limit by using strbufs here, too. Like
      sb_path, we pass the the buffers into our helper function,
      so that we can easily clean up all output paths. We can also
      drop the "unsafe" name from our helper function, as it no
      longer uses a single static buffer (but of course
      resolve_ref_unsafe is still unsafe, because the static
      buffers moved there).
      
      As a bonus, we also get to drop some strcpy calls between
      the two fixed buffers (that cannot currently overflow
      because the two buffers are sized identically).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      495127db
    • J
      read_remotes_file: simplify string handling · 0e265a92
      Jeff King 提交于
      The main motivation for this cleanup is to switch our
      line-reading to a strbuf, which removes the use of a
      fixed-size buffer (which limited the size of remote URLs).
      Since we have the strbuf, we can make use of strbuf_rtrim().
      
      While we're here, we can also simplify the parsing of each
      line.  First, we can use skip_prefix() to avoid some magic
      numbers.
      
      But second, we can avoid splitting the parsing and actions
      for each line into two stages. Right now we figure out which
      type of line we have, set an int to a magic number,
      skip any intermediate whitespace, and then act on
      the resulting value based on the magic number.
      
      Instead, let's factor the whitespace skipping into a
      function. That lets us avoid the magic numbers and keep the
      actions close to the parsing.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0e265a92
    • J
      read_branches_file: simplify string handling · f28e3ab2
      Jeff King 提交于
      This function does a lot of manual string handling, and has
      some unnecessary limits. This patch cleans up a number of
      things:
      
        1. Drop the arbitrary 1000-byte limit on the size of the
           remote name (we do not have such a limit in any of the
           other remote-reading mechanisms).
      
        2. Replace fgets into a fixed-size buffer with a strbuf,
           eliminating any limits on the length of the URL.
      
        3. Replace manual whitespace handling with strbuf_trim
           (since we now have a strbuf). This also gets rid
           of a call to strcpy, and the confusing reuse of the "p"
           pointer for multiple purposes.
      
        4. We currently build up the refspecs over multiple strbuf
           calls. We do this to handle the fact that the URL "frag"
           may not be present. But rather than have multiple
           conditionals, let's just default "frag" to "master".
           This lets us format the refspecs with a single xstrfmt.
           It's shorter, and easier to see what the final string
           looks like.
      
           We also update the misleading comment in this area (the
           local branch is named after the remote name, not after
           the branch name on the remote side).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      f28e3ab2
    • J
      mailmap: replace strcpy with xstrdup · c978610d
      Jeff King 提交于
      We want to make a copy of a string without any leading
      whitespace. To do so, we allocate a buffer large enough to
      hold the original, skip past the whitespace, then copy that.
      It's much simpler to just allocate after we've skipped, in
      which case we can just copy the remainder of the string,
      leaving no question of whether "len" is large enough.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c978610d
    • J
      help: drop prepend function in favor of xstrfmt · acd47eec
      Jeff King 提交于
      This function predates xstrfmt, and its functionality is a
      subset. Let's just use xstrfmt.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      acd47eec
    • J
      ref-filter: drop sprintf and strcpy calls · a5e03bf5
      Jeff King 提交于
      The ref-filter code comes from for-each-ref, and inherited a
      number of raw sprintf and strcpy calls. These are generally
      all safe, as we custom-size the buffers, or are formatting
      numbers into sufficiently large buffers. But we can make the
      resulting code even simpler and more obviously correct by
      using some of our helper functions.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      a5e03bf5
    • J
      use strip_suffix and xstrfmt to replace suffix · 9ae97018
      Jeff King 提交于
      When we want to convert "foo.pack" to "foo.idx", we do it by
      duplicating the original string and then munging the bytes
      in place. Let's use strip_suffix and xstrfmt instead, which
      has several advantages:
      
        1. It's more clear what the intent is.
      
        2. It does not implicitly rely on the fact that
           strlen(".idx") <= strlen(".pack") to avoid an overflow.
      
        3. We communicate the assumption that the input file ends
           with ".pack" (and get a run-time check that this is so).
      
        4. We drop calls to strcpy, which makes auditing the code
           base easier.
      
      Likewise, we can do this to convert ".pack" to ".bitmap",
      avoiding some manual memory computation.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      9ae97018
    • J
      fetch: replace static buffer with xstrfmt · 2805bb59
      Jeff King 提交于
      We parse the INFINITE_DEPTH constant into a static,
      fixed-size buffer using sprintf. This buffer is sufficiently
      large for the current constant, but it's a suspicious
      pattern, as the constant is defined far away, and it's not
      immediately obvious that 12 bytes are large enough to hold
      it.
      
      We can just use xstrfmt here, which gets rid of any question
      of the buffer size. It also removes any concerns with object
      lifetime, which means we do not have to wonder why this
      buffer deep within a conditional is marked "static" (we
      never free our newly allocated result, of course, but that's
      OK; it's global that lasts the lifetime of the whole program
      anyway).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      2805bb59
    • J
      config: use xstrfmt in normalize_value · 3ec832c4
      Jeff King 提交于
      We xmalloc a fixed-size buffer and sprintf into it; this is
      OK because the size of our formatting types is finite, but
      that's not immediately clear to a reader auditing sprintf
      calls. Let's switch to xstrfmt, which is shorter and
      obviously correct.
      
      Note that just dropping the common xmalloc here causes gcc
      to complain with -Wmaybe-uninitialized. That's because if
      "types" does not match any of our known types, we never
      write anything into the "normalized" pointer. With the
      current code, gcc doesn't notice because we always return a
      valid pointer (just one which might point to uninitialized
      data, but the compiler doesn't know that). In other words,
      the current code is potentially buggy if new types are added
      without updating this spot.
      
      So let's take this opportunity to clean up the function a
      bit more. We can drop the "normalized" pointer entirely, and
      just return directly from each code path. And then add an
      assertion at the end in case we haven't covered any cases.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3ec832c4
    • J
      replace trivial malloc + sprintf / strcpy calls with xstrfmt · 75faa45a
      Jeff King 提交于
      It's a common pattern to do:
      
        foo = xmalloc(strlen(one) + strlen(two) + 1 + 1);
        sprintf(foo, "%s %s", one, two);
      
      (or possibly some variant with strcpy()s or a more
      complicated length computation).  We can switch these to use
      xstrfmt, which is shorter, involves less error-prone manual
      computation, and removes many sprintf and strcpy calls which
      make it harder to audit the code for real buffer overflows.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      75faa45a
    • J
      receive-pack: convert strncpy to xsnprintf · b7115a35
      Jeff King 提交于
      This strncpy is pointless; we pass the strlen() of the src
      string, meaning that it works just like a memcpy. Worse,
      though, is that the size has no relation to the destination
      buffer, meaning it is a potential overflow.  In practice,
      it's not. We pass only short constant strings like
      "warning: " and "error: ", which are much smaller than the
      destination buffer.
      
      We can make this much simpler by just using xsnprintf, which
      will check for overflow and return the size for our next
      vsnprintf, without us having to run a separate strlen().
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      b7115a35
    • J
      http-push: replace strcat with xsnprintf · 0cc41428
      Jeff King 提交于
      We account for these strcats in our initial allocation, but
      the code is confusing to follow and verify. Let's remember
      our original allocation length, and then xsnprintf can
      verify that we don't exceed it.
      
      Note that we can't just use xstrfmt here (which would be
      even cleaner) because the code tries to grow the buffer only
      when necessary.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0cc41428
    • J
      add_packed_git: convert strcpy into xsnprintf · 48bcc1c3
      Jeff King 提交于
      We have the path "foo.idx", and we create a buffer big
      enough to hold "foo.pack" and "foo.keep", and then strcpy
      straight into it. This isn't a bug (we have enough space),
      but it's very hard to tell from the strcpy that this is so.
      
      Let's instead use strip_suffix to take off the ".idx",
      record the size of our allocation, and use xsnprintf to make
      sure we don't violate our assumptions.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      48bcc1c3
    • J
      entry.c: convert strcpy to xsnprintf · 330c8e26
      Jeff King 提交于
      This particular conversion is non-obvious, because nobody
      has passed our function the length of the destination
      buffer. However, the interface to checkout_entry specifies
      that the buffer must be at least TEMPORARY_FILENAME_LENGTH
      bytes long, so we can check that (meaning the existing code
      was not buggy, but merely worrisome to somebody reading it).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      330c8e26
    • J
      grep: use xsnprintf to format failure message · 19bdd3e7
      Jeff King 提交于
      This looks at first glance like the sprintf can overflow our
      buffer, but it's actually fine; the p->origin string is
      something constant and small, like "command line" or "-e
      option".
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      19bdd3e7
    • J
      compat/hstrerror: convert sprintf to snprintf · 48bdf869
      Jeff King 提交于
      This is a trivially correct use of sprintf, as our error
      number should not be excessively long. But it's still nice
      to drop an sprintf call.
      
      Note that we cannot use xsnprintf here, because this is
      compat code which does not load git-compat-util.h.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      48bdf869
    • J
      stop_progress_msg: convert sprintf to xsnprintf · f5691aa6
      Jeff King 提交于
      The usual arguments for using xsnprintf over sprintf apply,
      but this case is a little tricky. We print to a fixed-size
      buffer if we have room, and otherwise to an allocated
      buffer. So there should be no overflow here, but it is still
      good to communicate our intention, as well as to check our
      earlier math for how much space the string will need.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      f5691aa6
    • J
      find_short_object_filename: convert sprintf to xsnprintf · c3bb0ac7
      Jeff King 提交于
      We use sprintf() to format some hex data into a buffer. The
      buffer is clearly long enough, and using snprintf here is
      not necessary. And in fact, it does not really make anything
      easier to audit, as the size we feed to snprintf accounts
      for the magic extra 42 bytes found in each alt->name field
      of struct alternate_object_database (which is there exactly
      to do this formatting).
      
      Still, it is nice to remove an sprintf call and replace it
      with an xsnprintf and explanatory comment, which makes it
      easier to audit the code base for overflows.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c3bb0ac7
    • J
      use xsnprintf for generating git object headers · ef1286d3
      Jeff King 提交于
      We generally use 32-byte buffers to format git's "type size"
      header fields. These should not generally overflow unless
      you can produce some truly gigantic objects (and our types
      come from our internal array of constant strings). But it is
      a good idea to use xsnprintf to make sure this is the case.
      
      Note that we slightly modify the interface to
      write_sha1_file_prepare, which nows uses "hdrlen" as an "in"
      parameter as well as an "out" (on the way in it stores the
      allocated size of the header, and on the way out it returns
      the ultimate size of the header).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ef1286d3
    • J
      archive-tar: use xsnprintf for trivial formatting · f2f02675
      Jeff King 提交于
      When we generate tar headers, we sprintf() values directly
      into a struct with the fixed-size header values. For the
      most part this is fine, as we are formatting small values
      (e.g., the octal format of "mode & 0x7777" is of fixed
      length). But it's still a good idea to use xsnprintf here.
      It communicates to readers what our expectation is, and it
      provides a run-time check that we are not overflowing the
      buffers.
      
      The one exception here is the mtime, which comes from the
      epoch time of the commit we are archiving. For sane values,
      this fits into the 12-byte value allocated in the header.
      But since git can handle 64-bit times, if I claim to be a
      visitor from the year 10,000 AD, I can overflow the buffer.
      This turns out to be harmless, as we simply overflow into
      the chksum field, which is then overwritten.
      
      This case is also best as an xsnprintf. It should never come
      up, short of extremely malformed dates, and in that case we
      are probably better off dying than silently truncating the
      date value (and we cannot expand the size of the buffer,
      since it is dictated by the ustar format). Our friends in
      the year 5138 (when we legitimately flip to a 12-digit
      epoch) can deal with that problem then.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      f2f02675
    • J
      convert trivial sprintf / strcpy calls to xsnprintf · 5096d490
      Jeff King 提交于
      We sometimes sprintf into fixed-size buffers when we know
      that the buffer is large enough to fit the input (either
      because it's a constant, or because it's numeric input that
      is bounded in size). Likewise with strcpy of constant
      strings.
      
      However, these sites make it hard to audit sprintf and
      strcpy calls for buffer overflows, as a reader has to
      cross-reference the size of the array with the input. Let's
      use xsnprintf instead, which communicates to a reader that
      we don't expect this to overflow (and catches the mistake in
      case we do).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      5096d490
    • J
      compat/inet_ntop: fix off-by-one in inet_ntop4 · db85a8a9
      Jeff King 提交于
      Our compat inet_ntop4 function writes to a temporary buffer
      with snprintf, and then uses strcpy to put the result into
      the final "dst" buffer. We check the return value of
      snprintf against the size of "dst", but fail to account for
      the NUL terminator. As a result, we may overflow "dst" with
      a single NUL. In practice, this doesn't happen because the
      output of inet_ntop is limited, and we provide buffers that
      are way oversized.
      
      We can fix the off-by-one check easily, but while we are
      here let's also use strlcpy for increased safety, just in
      case there are other bugs lurking.
      
      As a side note, this compat code seems to be BSD-derived.
      Searching for "vixie inet_ntop" turns up NetBSD's latest
      version of the same code, which has an identical fix (and
      switches to strlcpy, too!).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      db85a8a9
    • J
      test-dump-cache-tree: avoid overflow of cache-tree name · 04724222
      Jeff King 提交于
      When dumping a cache-tree, we sprintf sub-tree names directly
      into a fixed-size buffer, which can overflow. We can
      trivially fix this by converting to xsnprintf to at least
      notice and die.
      
      This probably should handle arbitrary-sized names, but
      there's not much point. It's used only by the test scripts,
      so the trivial fix is enough.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      04724222
    • J
      progress: store throughput display in a strbuf · 3131977d
      Jeff King 提交于
      Coverity noticed that we strncpy() into a fixed-size buffer
      without making sure that it actually ended up
      NUL-terminated. This is unlikely to be a bug in practice,
      since throughput strings rarely hit 32 characters, but it
      would be nice to clean it up.
      
      The most obvious way to do so is to add a NUL-terminator.
      But instead, this patch switches the fixed-size buffer out
      for a strbuf. At first glance this seems much less
      efficient, until we realize that filling in the fixed-size
      buffer is done by writing into a strbuf and copying the
      result!
      
      By writing straight to the buffer, we actually end up more
      efficient:
      
        1. We avoid an extra copy of the bytes.
      
        2. Rather than malloc/free each time progress is shown, we
           can strbuf_reset and use the same buffer each time.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3131977d
    • J
      trace: use strbuf for quote_crnl output · 0bb443fd
      Jeff King 提交于
      When we output GIT_TRACE_SETUP paths, we quote any
      meta-characters. But our buffer to hold the result is only
      PATH_MAX bytes, and we could double the size of the input
      path (if every character needs quoting). We could use a
      2*PATH_MAX buffer, if we assume the input will never be more
      than PATH_MAX. But it's easier still to just switch to a
      strbuf and not worry about whether the input can exceed
      PATH_MAX or not.
      
      The original copied the "p2" pointer to "p1", advancing
      both. Since this gets rid of "p1", let's also drop "p2",
      whose name is now confusing. We can just advance the
      original "path" pointer.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0bb443fd
    • J
      mailsplit: make PATH_MAX buffers dynamic · 1d895f19
      Jeff King 提交于
      There are several PATH_MAX-sized buffers in mailsplit, along
      with some questionable uses of sprintf.  These are not
      really of security interest, as local mailsplit pathnames
      are not typically under control of an attacker, and you
      could generally only overflow a few numbers at the end of a
      path that approaches PATH_MAX (a longer path would choke
      mailsplit long before). But it does not hurt to be careful,
      and as a bonus we lift some limits for systems with
      too-small PATH_MAX varibles.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      1d895f19
    • J
      fsck: use strbuf to generate alternate directories · c1fd0809
      Jeff King 提交于
      When fsck-ing alternates, we make a copy of the alternate
      directory in a fixed PATH_MAX buffer. We memcpy directly,
      without any check whether we are overflowing the buffer.
      This is OK if PATH_MAX is a true representation of the
      maximum path on the system, because any path here will have
      already been vetted by the alternates subsystem. But that is
      not true on every system, so we should be more careful.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c1fd0809