Skip to contentSkip to conversation

compat: add a mkstemps() compatibility function

PATCH Superseded David Aguilar 3 participants 6 messages Jun 15, 2016 Kbuild

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 compat: add a mkstemps() compatibility function

mkstemps() is a BSD extension so provide an implementation for cross-platform use.

---
 Makefile          |   19 +++++++++++++++
 compat/mkstemps.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 config.mak.in     |    1 +
 configure.ac      |    6 ++++
 git-compat-util.h |    5 ++++
 5 files changed, 98 insertions(+), 0 deletions(-)
 create mode 100644 compat/mkstemps.c
Makefile +19 -0
index eaae45d..a70b5f0 100644
@@ -52,6 +52,8 @@ all::
 #
 # Define NO_MKDTEMP if you don't have mkdtemp in the C library.
 #
+# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
+#
 # Define NO_SYS_SELECT_H if you don't have sys/select.h.
 #
 # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -636,10 +638,12 @@ EXTLIBS =
 
 ifeq ($(uname_S),Linux)
 	NO_STRLCPY = YesPlease
+	NO_MKSTEMPS = YesPlease
 	THREADED_DELTA_SEARCH = YesPlease
 endif
 ifeq ($(uname_S),GNU/kFreeBSD)
 	NO_STRLCPY = YesPlease
+	NO_MKSTEMPS = YesPlease
 	THREADED_DELTA_SEARCH = YesPlease
 endif
 ifeq ($(uname_S),UnixWare)
@@ -651,6 +655,7 @@ ifeq ($(uname_S),UnixWare)
 	SHELL_PATH = /usr/local/bin/bash
 	NO_IPV6 = YesPlease
 	NO_HSTRERROR = YesPlease
+	NO_MKSTEMPS = YesPlease
 	BASIC_CFLAGS += -Kthread
 	BASIC_CFLAGS += -I/usr/local/include
 	BASIC_LDFLAGS += -L/usr/local/lib
@@ -674,6 +679,7 @@ ifeq ($(uname_S),SCO_SV)
 	SHELL_PATH = /usr/bin/bash
 	NO_IPV6 = YesPlease
 	NO_HSTRERROR = YesPlease
+	NO_MKSTEMPS = YesPlease
 	BASIC_CFLAGS += -I/usr/local/include
 	BASIC_LDFLAGS += -L/usr/local/lib
 	NO_STRCASESTR = YesPlease
@@ -702,6 +708,7 @@ ifeq ($(uname_S),SunOS)
 	NO_MEMMEM = YesPlease
 	NO_HSTRERROR = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_MKSTEMPS = YesPlease
 	OLD_ICONV = UnfortunatelyYes
 	ifeq ($(uname_R),5.8)
 		NO_UNSETENV = YesPlease
@@ -724,6 +731,7 @@ ifeq ($(uname_O),Cygwin)
 	NO_D_INO_IN_DIRENT = YesPlease
 	NO_STRCASESTR = YesPlease
 	NO_MEMMEM = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_SYMLINK_HEAD = YesPlease
 	NEEDS_LIBICONV = YesPlease
 	NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
@@ -767,11 +775,13 @@ ifeq ($(uname_S),NetBSD)
 	BASIC_LDFLAGS += -L/usr/pkg/lib $(CC_LD_DYNPATH)/usr/pkg/lib
 	THREADED_DELTA_SEARCH = YesPlease
 	USE_ST_TIMESPEC = YesPlease
+	NO_MKSTEMPS = YesPlease
 endif
 ifeq ($(uname_S),AIX)
 	NO_STRCASESTR=YesPlease
 	NO_MEMMEM = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_NSEC = YesPlease
 	FREAD_READS_DIRECTORIES = UnfortunatelyYes
@@ -787,12 +797,14 @@ endif
 ifeq ($(uname_S),GNU)
 	# GNU/Hurd
 	NO_STRLCPY=YesPlease
+	NO_MKSTEMPS = YesPlease
 endif
 ifeq ($(uname_S),IRIX64)
 	NO_IPV6=YesPlease
 	NO_SETENV=YesPlease
 	NO_STRCASESTR=YesPlease
 	NO_MEMMEM = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_SOCKADDR_STORAGE=YesPlease
 	SHELL_PATH=/usr/gnu/bin/bash
@@ -805,6 +817,7 @@ ifeq ($(uname_S),HP-UX)
 	NO_SETENV=YesPlease
 	NO_STRCASESTR=YesPlease
 	NO_MEMMEM = YesPlease
+	NO_MKSTEMPS = YesPlease
 	NO_STRLCPY = YesPlease
 	NO_MKDTEMP = YesPlease
 	NO_UNSETENV = YesPlease
@@ -834,6 +847,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 	NO_C99_FORMAT = YesPlease
 	NO_STRTOUMAX = YesPlease
 	NO_MKDTEMP = YesPlease
+	NO_MKSTEMPS = YesPlease
 	SNPRINTF_RETURNS_BOGUS = YesPlease
 	NO_SVN_TESTS = YesPlease
 	NO_PERL_MAKEMAKER = YesPlease
@@ -853,6 +867,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
 endif
 ifneq (,$(findstring arm,$(uname_M)))
 	ARM_SHA1 = YesPlease
+	NO_MKSTEMPS = YesPlease
 endif
 
 -include config.mak.autogen
@@ -1011,6 +1026,10 @@ ifdef NO_MKDTEMP
 	COMPAT_CFLAGS += -DNO_MKDTEMP
 	COMPAT_OBJS += compat/mkdtemp.o
 endif
+ifdef NO_MKSTEMPS
+	COMPAT_CFLAGS += -DNO_MKSTEMPS
+	COMPAT_OBJS += compat/mkstemps.o
+endif
 ifdef NO_UNSETENV
 	COMPAT_CFLAGS += -DNO_UNSETENV
 	COMPAT_OBJS += compat/unsetenv.o
compat/mkstemps.c new file +67 -0

67 lines not on this page · raw message

config.mak.in +1 -0
index 7cce0c1..b6619af 100644
@@ -46,6 +46,7 @@ NO_STRTOUMAX=@NO_STRTOUMAX@
 NO_SETENV=@NO_SETENV@
 NO_UNSETENV=@NO_UNSETENV@
 NO_MKDTEMP=@NO_MKDTEMP@
+NO_MKSTEMPS=@NO_MKSTEMPS@
 NO_ICONV=@NO_ICONV@
 OLD_ICONV=@OLD_ICONV@
 NO_DEFLATE_BOUND=@NO_DEFLATE_BOUND@
configure.ac +6 -0
index 4e728bc..95dccd4 100644
@@ -676,6 +676,12 @@ GIT_CHECK_FUNC(mkdtemp,
 [NO_MKDTEMP=],
 [NO_MKDTEMP=YesPlease])
 AC_SUBST(NO_MKDTEMP)
+# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
+GIT_CHECK_FUNC(mkstemps,
+[NO_MKSTEMPS=],
+[NO_MKSTEMPS=YesPlease])
+AC_SUBST(NO_MKSTEMPS)
+#
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
git-compat-util.h +5 -0
index c7cf2d5..f7217ad 100644
@@ -232,6 +232,11 @@ extern int gitsetenv(const char *, const char *, int);
 extern char *gitmkdtemp(char *);
 #endif
 
+#ifdef NO_MKSTEMPS
+#define mkstemps gitmkstemps
+extern int gitmkstemps(char *, int);
+#endif
+
 #ifdef NO_UNSETENV
 #define unsetenv gitunsetenv
 extern void gitunsetenv(const char *);
Signature · 1 line
1.6.3.1.169.g33fd
#1 lore raw

PATCH v3 2/2 diff: generate prettier filenames when using GIT_EXTERNAL_DIFF

Naturally, prep_temp_blob() did not care about filenames. As a result, scripts that use GIT_EXTERNAL_DIFF ended up with filenames such as ".diff_XXXXXX".

This specializes the GIT_EXTERNAL_DIFF code to generate user-friendly filenames when creating temporary files.

Diffing "name.ext" now generates "XXXXXX_name.ext".

---
 cache.h                  |    2 ++
 diff.c                   |   45 +++++++++++++++++++++++++++++++++++++--------
 path.c                   |   19 +++++++++++++++++++
 t/t4020-diff-external.sh |    9 +++++++++
 4 files changed, 67 insertions(+), 8 deletions(-)
cache.h +2 -0
index b8503ad..871c984 100644
@@ -614,6 +614,8 @@ extern int is_empty_blob_sha1(const unsigned char *sha1);
 
 int git_mkstemp(char *path, size_t n, const char *template);
 
+int git_mkstemps(char *path, size_t n, const char *template, int suffix_len);
+
 /*
  * NOTE NOTE NOTE!!
  *
diff.c +37 -8

86 lines not on this page · raw message

path.c +19 -0
index 8a0a674..090b490 100644
@@ -140,6 +140,25 @@ int git_mkstemp(char *path, size_t len, const char *template)
 }
 
 
+
+/* git_mkstemps() - create tmp file with suffix honoring TMPDIR variable. */
+int git_mkstemps(char *path, size_t len, const char *template, int suffix_len)
+{
+	const char *tmp;
+	size_t n;
+
+	tmp = getenv("TMPDIR");
+	if (!tmp)
+		tmp = "/tmp";
+	n = snprintf(path, len, "%s/%s", tmp, template);
+	if (len <= n) {
+		errno = ENAMETOOLONG;
+		return -1;
+	}
+	return mkstemps(path, suffix_len);
+}
+
+
 int validate_headref(const char *path)
 {
 	struct stat st;
t/t4020-diff-external.sh +9 -0
index 0720001..4ea42e0 100755
@@ -136,6 +136,15 @@ test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
 	GIT_EXTERNAL_DIFF=echo git diff
 '
 
+test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
+	touch file.ext &&
+	git add file.ext &&
+	echo with extension > file.ext &&
+	GIT_EXTERNAL_DIFF=echo git diff file.ext | grep ......_file\.ext &&
+	git update-index --force-remove file.ext &&
+	rm file.ext
+'
+
 echo "#!$SHELL_PATH" >fake-diff.sh
 cat >> fake-diff.sh <<\EOF
 cat $2 >> crlfed.txt
Signature · 1 line
1.6.3.1.169.g33fd
#2 lore raw
In reply to David Aguilar

Hi David

I think I see a few minor issues with the code dealing with random six letters generation. Please correct me if I am wrong.

Quoted 179 lines · David Aguilar

mkstemps() is a BSD extension so provide an implementation for cross-platform use.

---
 Makefile          |   19 +++++++++++++++
 compat/mkstemps.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ config.mak.in     |    1 +
 configure.ac      |    6 ++++
 git-compat-util.h |    5 ++++

 5 files changed, 98 insertions(+), 0 deletions(-)

 create mode 100644 compat/mkstemps.c

Makefile +2 -0
index eaae45d..a70b5f0 100644
@@ -52,6 +52,8 @@ all::
  #
  # Define NO_MKDTEMP if you don't have mkdtemp in the C library.
  #
+# Define NO_MKSTEMPS if you don't have mkstemps in the C library.
+#
  # Define NO_SYS_SELECT_H if you don't have sys/select.h.
  #
  # Define NO_SYMLINK_HEAD if you never want .git/HEAD to be a symbolic link.
@@ -636,10 +638,12 @@ EXTLIBS =
 
  ifeq ($(uname_S),Linux)

       NO_STRLCPY = YesPlease
+       NO_MKSTEMPS = YesPlease
       THREADED_DELTA_SEARCH = YesPlease
 endif
 ifeq ($(uname_S),GNU/kFreeBSD)
       NO_STRLCPY = YesPlease
+       NO_MKSTEMPS = YesPlease
       THREADED_DELTA_SEARCH = YesPlease
 endif
 ifeq ($(uname_S),UnixWare)

Makefile +0 -0
@@ -651,6 +655,7 @@ ifeq ($(uname_S),UnixWare)

       SHELL_PATH = /usr/local/bin/bash
       NO_IPV6 = YesPlease
       NO_HSTRERROR = YesPlease
+       NO_MKSTEMPS = YesPlease
       BASIC_CFLAGS += -Kthread
       BASIC_CFLAGS += -I/usr/local/include
       BASIC_LDFLAGS += -L/usr/local/lib

Makefile +0 -0
@@ -674,6 +679,7 @@ ifeq ($(uname_S),SCO_SV)

       SHELL_PATH = /usr/bin/bash
       NO_IPV6 = YesPlease
       NO_HSTRERROR = YesPlease
+       NO_MKSTEMPS = YesPlease
       BASIC_CFLAGS += -I/usr/local/include
       BASIC_LDFLAGS += -L/usr/local/lib
       NO_STRCASESTR = YesPlease

Makefile +0 -0
@@ -702,6 +708,7 @@ ifeq ($(uname_S),SunOS)

       NO_MEMMEM = YesPlease
       NO_HSTRERROR = YesPlease
       NO_MKDTEMP = YesPlease
+       NO_MKSTEMPS = YesPlease
       OLD_ICONV = UnfortunatelyYes
       ifeq ($(uname_R),5.8)
               NO_UNSETENV = YesPlease

Makefile +0 -0
@@ -724,6 +731,7 @@ ifeq ($(uname_O),Cygwin)

       NO_D_INO_IN_DIRENT = YesPlease NO_STRCASESTR = YesPlease
       NO_MEMMEM = YesPlease
+       NO_MKSTEMPS = YesPlease NO_SYMLINK_HEAD = YesPlease NEEDS_LIBICONV = YesPlease NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes

Makefile +0 -0
@@ -767,11 +775,13 @@ ifeq ($(uname_S),NetBSD)

       BASIC_LDFLAGS += -L/usr/pkg/lib $(CC_LD_DYNPATH)/usr/pkg/lib THREADED_DELTA_SEARCH = YesPlease
       USE_ST_TIMESPEC = YesPlease
+       NO_MKSTEMPS = YesPlease
 endif
 ifeq ($(uname_S),AIX)
       NO_STRCASESTR=YesPlease
       NO_MEMMEM = YesPlease
       NO_MKDTEMP = YesPlease
+       NO_MKSTEMPS = YesPlease
       NO_STRLCPY = YesPlease
       NO_NSEC = YesPlease
       FREAD_READS_DIRECTORIES = UnfortunatelyYes

Makefile +0 -0
@@ -787,12 +797,14 @@ endif
  ifeq ($(uname_S),GNU)
        # GNU/Hurd

       NO_STRLCPY=YesPlease
+       NO_MKSTEMPS = YesPlease
 endif
 ifeq ($(uname_S),IRIX64)
       NO_IPV6=YesPlease
       NO_SETENV=YesPlease
       NO_STRCASESTR=YesPlease
       NO_MEMMEM = YesPlease
+       NO_MKSTEMPS = YesPlease
       NO_STRLCPY = YesPlease
       NO_SOCKADDR_STORAGE=YesPlease
       SHELL_PATH=/usr/gnu/bin/bash

Makefile +0 -0
@@ -805,6 +817,7 @@ ifeq ($(uname_S),HP-UX)

       NO_SETENV=YesPlease
       NO_STRCASESTR=YesPlease
       NO_MEMMEM = YesPlease
+       NO_MKSTEMPS = YesPlease
       NO_STRLCPY = YesPlease
       NO_MKDTEMP = YesPlease
       NO_UNSETENV = YesPlease

Makefile +0 -0
@@ -834,6 +847,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))

       NO_C99_FORMAT = YesPlease
       NO_STRTOUMAX = YesPlease
       NO_MKDTEMP = YesPlease
+       NO_MKSTEMPS = YesPlease
       SNPRINTF_RETURNS_BOGUS = YesPlease
       NO_SVN_TESTS = YesPlease
       NO_PERL_MAKEMAKER = YesPlease

Makefile +0 -0
@@ -853,6 +867,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))

 endif
 ifneq (,$(findstring arm,$(uname_M)))
       ARM_SHA1 = YesPlease
+       NO_MKSTEMPS = YesPlease
 endif

 -include config.mak.autogen

Makefile +0 -0
@@ -1011,6 +1026,10 @@ ifdef NO_MKDTEMP

       COMPAT_CFLAGS += -DNO_MKDTEMP
       COMPAT_OBJS += compat/mkdtemp.o
 endif

Makefile +4 -0
+ifdef NO_MKSTEMPS
+       COMPAT_CFLAGS += -DNO_MKSTEMPS
+       COMPAT_OBJS += compat/mkstemps.o
+endif

 ifdef NO_UNSETENV
       COMPAT_CFLAGS += -DNO_UNSETENV
       COMPAT_OBJS += compat/unsetenv.o

compat/mkstemps.c new file +37 -0
new file mode 100644
index 0000000..87ebc2a
@@ -0,0 +1,67 @@
+#include "../git-compat-util.h"
+
+#ifndef TMP_MAX
+#define TMP_MAX 16384
+#endif
+
+/* Adapted from libiberty's mkstemp.c. */
+int gitmkstemps(char *pattern, int suffix_len)
+{
+       static const char letters[] =
+               "abcdefghijklmnopqrstuvwxyz"
+               "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+               "0123456789";
+       static const int num_letters = 62;
+       uint64_t value;
+       struct timeval tv;
+       char *template;
+       size_t len;
+       int fd, count;
+
+       len = strlen(pattern);
+
+       if (len < 6 + suffix_len) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       /* Replace pattern's XXXXXX characters with randomness.
+        * Try TMP_MAX different filenames.
+        */
+       gettimeofday(&tv, NULL);
+       value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();

compat/mkstemps.c line 47

Quoted 10 lines
compat/mkstemps.c new file +10 -0
+       template = &pattern[len - 6 - suffix_len];
+       for (count = 0; count < TMP_MAX; ++count) {
+               uint64_t v = value;
+               /* Fill in the random bits. */
+               template[0] = letters[v % num_letters]; v/= num_letters;
+               template[1] = letters[v % num_letters]; v/= num_letters;
+               template[2] = letters[v % num_letters]; v/= num_letters;
+               template[3] = letters[v % num_letters]; v/= num_letters;
+               template[4] = letters[v % num_letters]; v/= num_letters;
+               template[5] = letters[v % num_letters]; v/= num_letters;

v is divided 6 times by 62, and a decent amount of randomness shall be ensured only if 'value' is usually greater than 62^6.

If it is assumed to be ok for 'v' to become zero in the last one odd steps sometimes (in fact, _frequently_), then you may ignore what I am pointing out here.

1. On a couple of systems that I checked on, all variables/types on the right hand side of

      value = ((size_t)(tv.tv_usec << 16)) ^ tv.tv_sec ^ getpid();

are 4 bytes long. So 'value' is ultimately going to be assigned a number that fits in 4 bytes, ie value < 2^32 (at least on a few systems).
The systems that I checked on have these uname -a outputs:
- Linux host 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686
i686 i386 GNU/Linux
- Linux host 2.4.21-50.ELsmp #1 SMP Tue May 8 17:18:29 EDT 2007 i686
i686 i386 GNU/Linux

Also, even if size_t were 64 bits, typecasting _after_ the shift does not help much. And given the uncertainty about sizeof (size_t), we could use:
((uint64_t) tv.tv_usec) << 16)

2. tv_usec has a decimal value range of 0-999999 (10^6 usec make 1 sec). Which means that tv_usec fits completely in 20 bits (or less). (tv_usec << 16) yields a number that fits completely in 36 bits (or less). Max value of this number is 999999 * 2^16, or for convenience, about M = 10^6 * 2^16. This number (in the range of 0 to M) goes on to be divided by D=62^6. Also, M > D. Thus, there is about a D / M * 100 ~ 87 % probability of the division M / D working out to be zero. i.e, in 87% cases, the last division (v/= num_letters) will cause 'v' to become 0.
Therefore, I think an additional shift of 7 or more bits will help in improving randomness of template[5]. I would suggest left shifting by 24 bits. This seemed ok in some tests I did.

Combining #1 and #2, I guess we could have the computation of 'value' as:

      value = (((uint64_t)tv.tv_usec) << 24) ^ tv.tv_sec ^ getpid();

- Antriksh

#3 lore raw
Quoted 9 lines · Antriksh Pany

2. tv_usec has a decimal value range of 0-999999 (10^6 usec make 1 sec). Which means that tv_usec fits completely in 20 bits (or less). (tv_usec << 16) yields a number that fits completely in 36 bits (or less). Max value of this number is 999999 * 2^16, or for convenience, about M = 10^6 * 2^16. This number (in the range of 0 to M) goes on to be divided by D=62^6. Also, M > D. Thus, there is about a D / M * 100 ~ 87 % probability of the division M / D working out to be zero. i.e, in 87% cases, the last division (v/= num_letters) will cause 'v' to become 0.

But this value (after the 6th division, mind you) is not used. v is used the last time after the 5th division; at this time it still carries enough
randomness: 62^5 < 2^32.

-- Hannes

#4 lore raw

Oh ok, didn't realize the sixth division was not used. In which case, we needn't have the sixth division.

Also, 62^5 compared to 2^32 still gives a ~ 21% probability of the fifth division becoming zero, fairly high perhaps.

The suggestion to change the code to
((uint64_t) tv.tv_usec) << 16)
might help here. This would still leave about a 1.4% chance of v becoming zero, higher than (being pedantic!) what perfect randomness would require. Livable. But (just curious), do we see any loss in shifting a few more positions?

- Antriksh

Quoted 18 lines · Johannes Sixt
Quoted 9 lines · Antriksh Pany

2. tv_usec has a decimal value range of 0-999999  (10^6 usec make 1 sec). Which means that tv_usec fits completely in 20 bits (or less). (tv_usec << 16) yields a number that fits completely in 36 bits (or less). Max value of this number is 999999 * 2^16, or for convenience, about M = 10^6 * 2^16. This number (in the range of 0 to M) goes on to be divided by D=62^6. Also, M > D. Thus, there is about a  D / M * 100 ~ 87 % probability of the division M / D working out to be zero. i.e, in 87% cases, the last division (v/= num_letters) will cause 'v' to become 0.

But this value (after the 6th division, mind you) is not used. v is used the last time after the 5th division; at this time it still carries enough
randomness: 62^5 < 2^32.

-- Hannes

#5 lore raw
Quoted 6 lines · Antriksh Pany

Oh ok, didn't realize the sixth division was not used. In which case, we needn't have the sixth division.

Also, 62^5 compared to 2^32 still gives a ~ 21% probability of the fifth division becoming zero, fairly high perhaps.

Quoted 8 lines

The suggestion to change the code to
((uint64_t) tv.tv_usec) << 16)
might help here. This would still leave about a 1.4% chance of v becoming zero, higher than (being pedantic!) what perfect randomness would require. Livable. But (just curious), do we see any loss in shifting a few more positions?

- Antriksh

Just a small note -- our mkstemps was adapted from libiberty's mkstemps.c (gcc-4.something), so if there is indeed something to fix here then we should let them know.

I looked in libiberty since I assumed that the code in there was battle-tested and stable.

I feel okay with it as-is, but that's just me.

Quoted 19 lines · Johannes Sixt
Quoted 18 lines
Quoted 9 lines · Antriksh Pany

2. tv_usec has a decimal value range of 0-999999  (10^6 usec make 1 sec). Which means that tv_usec fits completely in 20 bits (or less). (tv_usec << 16) yields a number that fits completely in 36 bits (or less). Max value of this number is 999999 * 2^16, or for convenience, about M = 10^6 * 2^16. This number (in the range of 0 to M) goes on to be divided by D=62^6. Also, M > D. Thus, there is about a  D / M * 100 ~ 87 % probability of the division M / D working out to be zero. i.e, in 87% cases, the last division (v/= num_letters) will cause 'v' to become 0.

But this value (after the 6th division, mind you) is not used. v is used the last time after the 5th division; at this time it still carries enough
randomness: 62^5 < 2^32.

-- Hannes

Signature · 1 line
		David