1. 23 2月, 2016 3 次提交
    • J
      fast-import: simplify allocation in start_packfile · a78c188a
      Jeff King 提交于
      This function allocate a packed_git flex-array, and adds a
      mysterious 2 bytes to the length of the pack_name field. One
      is for the trailing NUL, but the other has no purpose. This
      is probably cargo-culted from add_packed_git, which gets the
      ".idx" path and needed to allocate enough space to hold the
      matching ".pack" (though since 48bcc1c3, we calculate the
      size there differently).
      
      This site, however, is using the raw path of a tempfile, and
      does not need the extra byte. We can just replace the
      allocation with FLEX_ALLOC_STR, which handles the allocation
      and the NUL for us.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      a78c188a
    • J
      use st_add and st_mult for allocation size computation · 50a6c8ef
      Jeff King 提交于
      If our size computation overflows size_t, we may allocate a
      much smaller buffer than we expected and overflow it. It's
      probably impossible to trigger an overflow in most of these
      sites in practice, but it is easy enough convert their
      additions and multiplications into overflow-checking
      variants. This may be fixing real bugs, and it makes
      auditing the code easier.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      50a6c8ef
    • J
      convert trivial cases to ALLOC_ARRAY · b32fa95f
      Jeff King 提交于
      Each of these cases can be converted to use ALLOC_ARRAY or
      REALLOC_ARRAY, which has two advantages:
      
        1. It automatically checks the array-size multiplication
           for overflow.
      
        2. It always uses sizeof(*array) for the element-size,
           so that it can never go out of sync with the declared
           type of the array.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      b32fa95f
  2. 16 1月, 2016 1 次提交
    • J
      strbuf: introduce strbuf_getline_{lf,nul}() · 8f309aeb
      Junio C Hamano 提交于
      The strbuf_getline() interface allows a byte other than LF or NUL as
      the line terminator, but this is only because I wrote these
      codepaths anticipating that there might be a value other than NUL
      and LF that could be useful when I introduced line_termination long
      time ago.  No useful caller that uses other value has emerged.
      
      By now, it is clear that the interface is overly broad without a
      good reason.  Many codepaths have hardcoded preference to read
      either LF terminated or NUL terminated records from their input, and
      then call strbuf_getline() with LF or NUL as the third parameter.
      
      This step introduces two thin wrappers around strbuf_getline(),
      namely, strbuf_getline_lf() and strbuf_getline_nul(), and
      mechanically rewrites these call sites to call either one of
      them.  The changes contained in this patch are:
      
       * introduction of these two functions in strbuf.[ch]
      
       * mechanical conversion of all callers to strbuf_getline() with
         either '\n' or '\0' as the third parameter to instead call the
         respective thin wrapper.
      
      After this step, output from "git grep 'strbuf_getline('" would
      become a lot smaller.  An interim goal of this series is to make
      this an empty set, so that we can have strbuf_getline_crlf() take
      over the shorter name strbuf_getline().
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      8f309aeb
  3. 02 12月, 2015 1 次提交
    • S
      Make error message after failing commit_lock_file() less confusing · 08a3651f
      SZEDER Gábor 提交于
      The error message after a failing commit_lock_file() call sometimes
      looks like this, causing confusion:
      
        $ git remote add remote git@server.com/repo.git
        error: could not commit config file .git/config
        # Huh?!
        # I didn't want to commit anything, especially not my config file!
      
      While in the narrow context of the lockfile module using the verb
      'commit' in the error message makes perfect sense, in the broader
      context of git the word 'commit' already has a very specific meaning,
      hence the confusion.
      
      Reword these error messages to say "could not write" instead of "could
      not commit".
      
      While at it, include strerror in the error messages after writing the
      config file or the credential store fails to provide some information
      about the cause of the failure, and update the style of the error
      message after writing the reflog fails to match surrounding error
      messages (i.e. no '' around the pathname and no () around the error
      description).
      Signed-off-by: NSZEDER Gábor <szeder@ira.uka.de>
      Signed-off-by: NJeff King <peff@peff.net>
      08a3651f
  4. 06 10月, 2015 3 次提交
    • J
      convert strncpy to memcpy · eddda371
      Jeff King 提交于
      strncpy is known to be a confusing function because of its
      termination semantics.  These calls are all correct, but it
      takes some examination to see why. In particular, every one
      of them expects to copy up to the length limit, and then
      makes some arrangement for terminating the result.
      
      We can just use memcpy, along with noting explicitly how the
      result is terminated (if it is not already obvious). That
      should make it more clear to a reader that we are doing the
      right thing.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      eddda371
    • J
      prefer memcpy to strcpy · 34fa79a6
      Jeff King 提交于
      When we already know the length of a string (e.g., because
      we just malloc'd to fit it), it's nicer to use memcpy than
      strcpy, as it makes it more obvious that we are not going to
      overflow the buffer (because the size we pass matches the
      size in the allocation).
      
      This also eliminates calls to strcpy, which make auditing
      the code base harder.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      34fa79a6
    • J
      avoid sprintf and strcpy with flex arrays · c7ab0ba3
      Jeff King 提交于
      When we are allocating a struct with a FLEX_ARRAY member, we
      generally compute the size of the array and then sprintf or
      strcpy into it. Normally we could improve a dynamic allocation
      like this by using xstrfmt, but it doesn't work here; we
      have to account for the size of the rest of the struct.
      
      But we can improve things a bit by storing the length that
      we use for the allocation, and then feeding it to xsnprintf
      or memcpy, which makes it more obvious that we are not
      writing more than the allocated number of bytes.
      
      It would be nice if we had some kind of helper for
      allocating generic flex arrays, but it doesn't work that
      well:
      
       - the call signature is a little bit unwieldy:
      
            d = flex_struct(sizeof(*d), offsetof(d, path), fmt, ...);
      
         You need offsetof here instead of just writing to the
         end of the base size, because we don't know how the
         struct is packed (partially this is because FLEX_ARRAY
         might not be zero, though we can account for that; but
         the size of the struct may actually be rounded up for
         alignment, and we can't know that).
      
       - some sites do clever things, like over-allocating because
         they know they will write larger things into the buffer
         later (e.g., struct packed_git here).
      
      So we're better off to just write out each allocation (or
      add type-specific helpers, though many of these are one-off
      allocations anyway).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c7ab0ba3
  5. 26 9月, 2015 1 次提交
    • 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
  6. 04 9月, 2015 1 次提交
    • J
      fast-import: switch crash-report date to iso8601 · 547ed716
      Jeff King 提交于
      When fast-import emits a crash report, it does so in the
      user's local timezone. But because we omit the timezone
      completely for DATE_LOCAL, a reader of the report does not
      immediately know which time zone was used. Let's switch this
      to ISO8601 instead, which includes the time zone.
      
      This does mean we will show the time in UTC, but that's not
      a big deal. A crash report like this will either be looked
      at immediately (in which case nobody even looks at the
      timestamp), or it will be passed along to a developer to
      debug, in which case the original timezone is less likely to
      be of interest.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJohn Keeping <john@keeping.me.uk>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      547ed716
  7. 11 8月, 2015 2 次提交
    • J
      prefer git_pathdup to git_path in some possibly-dangerous cases · fcd12db6
      Jeff King 提交于
      Because git_path uses a static buffer that is shared with
      calls to git_path, mkpath, etc, it can be dangerous to
      assign the result to a variable or pass it to a non-trivial
      function. The value may change unexpectedly due to other
      calls.
      
      None of the cases changed here has a known bug, but they're
      worth converting away from git_path because:
      
        1. It's easy to use git_pathdup in these cases.
      
        2. They use constructs (like assignment) that make it
           hard to tell whether they're safe or not.
      
      The extra malloc overhead should be trivial, as an
      allocation should be an order of magnitude cheaper than a
      system call (which we are clearly about to make, since we
      are constructing a filename). The real cost is that we must
      remember to free the result.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      fcd12db6
    • J
      sha1_file.c: rename move_temp_to_file() to finalize_object_file() · cb5add58
      Junio C Hamano 提交于
      Since 5a688fe4 ("core.sharedrepository = 0mode" should set, not
      loosen, 2009-03-25), we kept reminding ourselves:
      
          NEEDSWORK: this should be renamed to finalize_temp_file() as
          "moving" is only a part of what it does, when no patch between
          master to pu changes the call sites of this function.
      
      without doing anything about it.  Let's do so.
      
      The purpose of this function was not to move but to finalize.  The
      detail of the primarily implementation of finalizing was to link the
      temporary file to its final name and then to unlink, which wasn't
      even "moving".  The alternative implementation did "move" by calling
      rename(2), which is a fun tangent.
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      cb5add58
  8. 14 7月, 2015 1 次提交
    • M
      fast-import: do less work when given "from" matches current branch head · 0df32457
      Mike Hommey 提交于
      When building a fast-import stream, it's easy to forget the fact
      that for non-merge commits happening on top of the current branch
      head, there is no need for a "from" command. That is corroborated by
      the fact that at least git-p4, hg-fast-export and felipec's
      git-remote-hg all unconditionally use a "from" command.
      
      Unfortunately, giving a "from" command always resets the branch
      tree, forcing it to be re-read, and in many cases, the pack is also
      closed and reopened through gfi_unpack_entry.  Both are unnecessary
      overhead, and the latter is particularly slow at least on OSX.
      
      Avoid resetting the tree when it's unmodified, and avoid calling
      gfi_unpack_entry when the given mark points to the same commit as
      the current branch head.
      Signed-off-by: NMike Hommey <mh@glandium.org>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      0df32457
  9. 02 7月, 2015 1 次提交
    • M
      fast-import: add a get-mark command · 28c7b1f7
      Michael Haggerty 提交于
      It is sometimes useful for importers to be able to read the SHA-1
      corresponding to a mark that they have created via fast-import. For
      example, they might want to embed the SHA-1 into the commit message of
      a later commit. Or it might be useful for internal bookkeeping uses,
      or for logging.
      
      Add a "get-mark" command to "git fast-import" that allows the importer
      to ask for the value of a mark that has been created earlier.
      Signed-off-by: NMichael Haggerty <mhagger@alum.mit.edu>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      28c7b1f7
  10. 30 6月, 2015 1 次提交
    • J
      convert "enum date_mode" into a struct · a5481a6c
      Jeff King 提交于
      In preparation for adding date modes that may carry extra
      information beyond the mode itself, this patch converts the
      date_mode enum into a struct.
      
      Most of the conversion is fairly straightforward; we pass
      the struct as a pointer and dereference the type field where
      necessary. Locations that declare a date_mode can use a "{}"
      constructor.  However, the tricky case is where we use the
      enum labels as constants, like:
      
        show_date(t, tz, DATE_NORMAL);
      
      Ideally we could say:
      
        show_date(t, tz, &{ DATE_NORMAL });
      
      but of course C does not allow that. Likewise, we cannot
      cast the constant to a struct, because we need to pass an
      actual address. Our options are basically:
      
        1. Manually add a "struct date_mode d = { DATE_NORMAL }"
           definition to each caller, and pass "&d". This makes
           the callers uglier, because they sometimes do not even
           have their own scope (e.g., they are inside a switch
           statement).
      
        2. Provide a pre-made global "date_normal" struct that can
           be passed by address. We'd also need "date_rfc2822",
           "date_iso8601", and so forth. But at least the ugliness
           is defined in one place.
      
        3. Provide a wrapper that generates the correct struct on
           the fly. The big downside is that we end up pointing to
           a single global, which makes our wrapper non-reentrant.
           But show_date is already not reentrant, so it does not
           matter.
      
      This patch implements 3, along with a minor macro to keep
      the size of the callers sane.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      a5481a6c
  11. 23 6月, 2015 1 次提交
  12. 06 3月, 2015 1 次提交
  13. 18 2月, 2015 1 次提交
  14. 11 2月, 2015 1 次提交
    • J
      fast-import: avoid running end_packfile recursively · 5e915f30
      Jeff King 提交于
      When an import has finished, we run end_packfile() to
      finalize the data and move the packfile into place. If this
      process fails, we call die() and end up in our die_nicely()
      handler.  Which unfortunately includes running end_packfile
      to save any progress we made. We enter the function again,
      and start operating on the pack_data struct while it is in
      an inconsistent state, leading to a segfault.
      
      One way to trigger this is to simply start two identical
      fast-imports at the same time. They will both create the
      same packfiles, which will then try to create identically
      named ".keep" files. One will win the race, and the other
      will die(), and end up with the segfault.
      
      Since 3c078b9c, we already reset the pack_data pointer to
      NULL at the end of end_packfile. That covers the case of us
      calling die() right after end_packfile, before we have
      reinitialized the pack_data pointer. This new problem is
      quite similar, except that we are worried about calling
      die() _during_ end_packfile, not right after. Ideally we
      would simply set pack_data to NULL as soon as we enter the
      function, and operate on a copy of the pointer.
      
      Unfortunately, it is not so easy. pack_data is a global, and
      end_packfile calls into other functions which operate on the
      global directly. We would have to teach each of these to
      take an argument, and there is no guarantee that we would
      catch all of the spots.
      
      Instead, we can simply use a static flag to avoid
      recursively entering the function. This is a little less
      elegant, but it's short and fool-proof.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      5e915f30
  15. 02 12月, 2014 2 次提交
  16. 16 10月, 2014 1 次提交
  17. 02 10月, 2014 3 次提交
  18. 19 9月, 2014 1 次提交
  19. 04 9月, 2014 2 次提交
  20. 30 8月, 2014 1 次提交
  21. 28 8月, 2014 1 次提交
    • J
      date: use strbufs in date-formatting functions · c33ddc2e
      Jeff King 提交于
      Many of the date functions write into fixed-size buffers.
      This is a minor pain, as we have to take special
      precautions, and frequently end up copying the result into a
      strbuf or heap-allocated buffer anyway (for which we
      sometimes use strcpy!).
      
      Let's instead teach parse_date, datestamp, etc to write to a
      strbuf. The obvious downside is that we might need to
      perform a heap allocation where we otherwise would not need
      to. However, it turns out that the only two new allocations
      required are:
      
        1. In test-date.c, where we don't care about efficiency.
      
        2. In determine_author_info, which is not performance
           critical (and where the use of a strbuf will help later
           refactoring).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c33ddc2e
  22. 26 8月, 2014 2 次提交
    • J
      fast-import: fix buffer overflow in dump_tags · c2527859
      Jeff King 提交于
      When creating a new annotated tag, we sprintf the refname
      into a static-sized buffer. If we have an absurdly long
      tagname, like:
      
        git init repo &&
        cd repo &&
        git commit --allow-empty -m foo &&
        git tag -m message mytag &&
        git fast-export mytag |
        perl -lpe '/^tag/ and s/mytag/"a" x 8192/e' |
        git fast-import <input
      
      we'll overflow the buffer. We can fix it by using a strbuf.
      Signed-off-by: NJeff King <peff@peff.net>
      Reviewed-by: NMichael Haggerty <mhagger@alum.mit.edu>
      Reviewed-by: NRonnie Sahlberg <sahlberg@google.com>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      c2527859
    • J
      fast-import: clean up pack_data pointer in end_packfile · 3c078b9c
      Jeff King 提交于
      We have a global pointer pack_data pointing to the current
      pack we have open. Inside end_packfile we have two new
      pointers, old_p and new_p. The latter points to pack_data,
      and the former points to the new "installed" version of the
      packfile we get when we hand the file off to the regular
      sha1_file machinery. When then free old_p.
      
      Presumably the extra old_p pointer was there so that we
      could overwrite pack_data with new_p and still free old_p,
      but we don't do that. We just leave pack_data pointing to
      bogus memory, and don't overwrite it until we call
      start_packfile again (if ever).
      
      This can cause problems for our die routine, which calls
      end_packfile to clean things up. If we die at the wrong
      moment, we can end up looking at invalid memory in
      pack_data left after the last end_packfile().
      
      Instead, let's make sure we set pack_data to NULL after we
      free it, and make calling endfile() again with a NULL
      pack_data a noop (there is nothing to end).
      
      We can further make things less confusing by dropping old_p
      entirely, and moving new_p closer to its point of use.
      Signed-off-by: NJeff King <peff@peff.net>
      Reviewed-by: NRonnie Sahlberg <sahlberg@google.com>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      3c078b9c
  23. 14 8月, 2014 1 次提交
  24. 19 7月, 2014 1 次提交
  25. 21 6月, 2014 4 次提交
    • J
      fast-import: refactor parsing of spaces · e814c39c
      Jeff King 提交于
      When we see a file change in a commit, we expect one of:
      
        1. A mark.
      
        2. An "inline" keyword.
      
        3. An object sha1.
      
      The handling of spaces is inconsistent between the three
      options. Option 1 calls a sub-function which checks for the
      space, but doesn't parse past it. Option 2 parses the space,
      then deliberately avoids moving the pointer past it. Option
      3 detects the space locally but doesn't move past it.
      
      This is confusing, because it looks like option 1 forgets to
      check for the space (it's just buried). And option 2 checks
      for "inline ", but only moves strlen("inline") characters
      forward, which looks like a bug but isn't.
      
      We can make this more clear by just having each branch move
      past the space as it is checked (and we can replace the
      doubled use of "inline" with a call to skip_prefix).
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      e814c39c
    • J
      fast-import: use skip_prefix for parsing input · 97313bef
      Jeff King 提交于
      Fast-import does a lot of parsing of commands and
      dispatching to sub-functions. For example, given "option
      foo", we might recognize "option " using starts_with, and
      then hand it off to parse_option() to do the rest.
      
      However, we do not let parse_option know that we have parsed
      the first part already. It gets the full buffer, and has to
      skip past the uninteresting bits. Some functions simply add
      a magic constant:
      
        char *option = command_buf.buf + 7;
      
      Others use strlen:
      
        char *option = command_buf.buf + strlen("option ");
      
      And others use strchr:
      
        char *option = strchr(command_buf.buf, ' ') + 1;
      
      All of these are brittle and easy to get wrong (especially
      given that the starts_with call and the code that assumes
      the presence of the prefix are far apart). Instead, we can
      use skip_prefix, and just pass each handler a pointer to its
      arguments.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      97313bef
    • J
      use skip_prefix to avoid magic numbers · ae021d87
      Jeff King 提交于
      It's a common idiom to match a prefix and then skip past it
      with a magic number, like:
      
        if (starts_with(foo, "bar"))
      	  foo += 3;
      
      This is easy to get wrong, since you have to count the
      prefix string yourself, and there's no compiler check if the
      string changes.  We can use skip_prefix to avoid the magic
      numbers here.
      
      Note that some of these conversions could be much shorter.
      For example:
      
        if (starts_with(arg, "--foo=")) {
      	  bar = arg + 6;
      	  continue;
        }
      
      could become:
      
        if (skip_prefix(arg, "--foo=", &bar))
      	  continue;
      
      However, I have left it as:
      
        if (skip_prefix(arg, "--foo=", &v)) {
      	  bar = v;
      	  continue;
        }
      
      to visually match nearby cases which need to actually
      process the string. Like:
      
        if (skip_prefix(arg, "--foo=", &v)) {
      	  bar = atoi(v);
      	  continue;
        }
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ae021d87
    • J
      fast-import: fix read of uninitialized argv memory · ff45c0d4
      Jeff King 提交于
      Fast-import shares code between its command-line parser and
      the "option" command. To do so, it strips the "--" from any
      command-line options and passes them to the option parser.
      However, it does not confirm that the option even begins
      with "--" before blindly passing "arg + 2".
      
      It does confirm that the option starts with "-", so the only
      affected case was:
      
        git fast-import -
      
      which would read uninitialized memory after the argument. We
      can fix it by using skip_prefix and checking the result. As
      a bonus, this gets rid of some magic numbers.
      Signed-off-by: NJeff King <peff@peff.net>
      Signed-off-by: NJunio C Hamano <gitster@pobox.com>
      ff45c0d4
  26. 22 4月, 2014 1 次提交
  27. 10 3月, 2014 1 次提交