Skip to content

Commit 53d687b

Browse files
peffgitster
authored andcommitted
git_mkstemps_mode(): replace magic numbers with computed value
The magic number "6" appears several times in the function, and is related to the size of the "XXXXXX" string we expect to find in the template. Let's pull that "XXXXXX" into a constant array, whose size we can get at compile time with ARRAY_SIZE(). Note that we probably can't just change this value, since callers will be feeding us a certain number of X's, but it hopefully makes the function itself easier to follow. While we're here, let's do the same with the "letters" array (which we _could_ modify if we wanted to include more characters). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54a80a9 commit 53d687b

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

wrapper.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,9 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
478478
"abcdefghijklmnopqrstuvwxyz"
479479
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
480480
"0123456789";
481-
static const int num_letters = 62;
481+
static const int num_letters = ARRAY_SIZE(letters) - 1;
482+
static const char x_pattern[] = "XXXXXX";
483+
static const int num_x = ARRAY_SIZE(x_pattern) - 1;
482484
uint64_t value;
483485
struct timeval tv;
484486
char *filename_template;
@@ -487,12 +489,12 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
487489

488490
len = strlen(pattern);
489491

490-
if (len < 6 + suffix_len) {
492+
if (len < num_x + suffix_len) {
491493
errno = EINVAL;
492494
return -1;
493495
}
494496

495-
if (strncmp(&pattern[len - 6 - suffix_len], "XXXXXX", 6)) {
497+
if (strncmp(&pattern[len - num_x - suffix_len], x_pattern, num_x)) {
496498
errno = EINVAL;
497499
return -1;
498500
}
@@ -503,12 +505,12 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode)
503505
*/
504506
gettimeofday(&tv, NULL);
505507
value = ((uint64_t)tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
506-
filename_template = &pattern[len - 6 - suffix_len];
508+
filename_template = &pattern[len - num_x - suffix_len];
507509
for (count = 0; count < TMP_MAX; ++count) {
508510
uint64_t v = value;
509511
int i;
510512
/* Fill in the random bits. */
511-
for (i = 0; i < 6; i++) {
513+
for (i = 0; i < num_x; i++) {
512514
filename_template[i] = letters[v % num_letters];
513515
v /= num_letters;
514516
}

0 commit comments

Comments
 (0)