Code

fast-import: Fix crash when referencing already existing objects
[git.git] / git-compat-util.h
index 2c84016ac9942eb62c24d143a551c8f1c8f48bb6..6bd8987b2774774fbbd3747a2b571b66ad78727b 100644 (file)
@@ -21,6 +21,9 @@
 
 #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
 
+/* Approximation of the length of the decimal representation of this type. */
+#define decimal_length(x)      ((int)(sizeof(x) * 2.56 + 0.5) + 1)
+
 #if !defined(__APPLE__) && !defined(__FreeBSD__)
 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
@@ -197,6 +200,19 @@ static inline void *xmalloc(size_t size)
        return ret;
 }
 
+static inline char *xstrndup(const char *str, size_t len)
+{
+       char *p;
+
+       p = memchr(str, '\0', len);
+       if (p)
+               len = p - str;
+       p = xmalloc(len + 1);
+       memcpy(p, str, len);
+       p[len] = '\0';
+       return p;
+}
+
 static inline void *xrealloc(void *ptr, size_t size)
 {
        void *ret = realloc(ptr, size);