diff --git a/include/git2/config.h b/include/git2/config.h index bafac6804..36647591a 100644 --- a/include/git2/config.h +++ b/include/git2/config.h @@ -166,7 +166,7 @@ GIT_EXTERN(void) git_config_free(git_config *cfg); * @param out pointer to the variable where the value should be stored * @return GIT_SUCCESS or an error code */ -GIT_EXTERN(int) git_config_get_int(git_config *cfg, const char *name, int *out); +GIT_EXTERN(int) git_config_get_int32(git_config *cfg, const char *name, int32_t *out); /** * Get the value of a long integer config variable. @@ -176,7 +176,7 @@ GIT_EXTERN(int) git_config_get_int(git_config *cfg, const char *name, int *out); * @param out pointer to the variable where the value should be stored * @return GIT_SUCCESS or an error code */ -GIT_EXTERN(int) git_config_get_long(git_config *cfg, const char *name, long long *out); +GIT_EXTERN(int) git_config_get_int64(git_config *cfg, const char *name, int64_t *out); /** * Get the value of a boolean config variable. @@ -212,7 +212,7 @@ GIT_EXTERN(int) git_config_get_string(git_config *cfg, const char *name, const c * @param value Integer value for the variable * @return GIT_SUCCESS or an error code */ -GIT_EXTERN(int) git_config_set_int(git_config *cfg, const char *name, int value); +GIT_EXTERN(int) git_config_set_int32(git_config *cfg, const char *name, int32_t value); /** * Set the value of a long integer config variable. @@ -222,7 +222,7 @@ GIT_EXTERN(int) git_config_set_int(git_config *cfg, const char *name, int value) * @param value Long integer value for the variable * @return GIT_SUCCESS or an error code */ -GIT_EXTERN(int) git_config_set_long(git_config *cfg, const char *name, long long value); +GIT_EXTERN(int) git_config_set_int64(git_config *cfg, const char *name, int64_t value); /** * Set the value of a boolean config variable. diff --git a/src/config.c b/src/config.c index 54727c0f6..f53afa145 100644 --- a/src/config.c +++ b/src/config.c @@ -164,16 +164,16 @@ int git_config_delete(git_config *cfg, const char *name) * Setters **************/ -int git_config_set_long(git_config *cfg, const char *name, long long value) +int git_config_set_int64(git_config *cfg, const char *name, int64_t value) { char str_value[32]; /* All numbers should fit in here */ p_snprintf(str_value, sizeof(str_value), "%" PRId64, value); return git_config_set_string(cfg, name, str_value); } -int git_config_set_int(git_config *cfg, const char *name, int value) +int git_config_set_int32(git_config *cfg, const char *name, int32_t value) { - return git_config_set_long(cfg, name, (long long)value); + return git_config_set_int64(cfg, name, (int64_t)value); } int git_config_set_bool(git_config *cfg, const char *name, int value) @@ -199,11 +199,11 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value) * Getters ***********/ -int git_config_get_long(git_config *cfg, const char *name, long long *out) +int git_config_get_int64(git_config *cfg, const char *name, int64_t *out) { const char *value, *num_end; int ret; - long long num; + int64_t num; ret = git_config_get_string(cfg, name, &value); if (ret < GIT_SUCCESS) @@ -214,35 +214,45 @@ int git_config_get_long(git_config *cfg, const char *name, long long *out) return git__rethrow(ret, "Failed to convert value for '%s'", name); switch (*num_end) { - case '\0': - break; + case 'g': + case 'G': + num *= 1024; + /* fallthrough */ + + case 'm': + case 'M': + num *= 1024; + /* fallthrough */ + case 'k': case 'K': num *= 1024; - break; - case 'm': - case 'M': - num *= 1024 * 1024; - break; - case 'g': - case 'G': - num *= 1024 * 1024 * 1024; - break; + + /* check that that there are no more characters after the + * given modifier suffix */ + if (num_end[1] != '\0') + return git__throw(GIT_EINVALIDTYPE, + "Failed to get value for '%s'. Invalid type suffix", name); + + /* fallthrough */ + + case '\0': + *out = num; + return GIT_SUCCESS; + default: - return git__throw(GIT_EINVALIDTYPE, "Failed to get value for '%s'. Value is of invalid type", name); + return git__throw(GIT_EINVALIDTYPE, + "Failed to get value for '%s'. Value is of invalid type", name); } - - *out = num; - - return GIT_SUCCESS; } -int git_config_get_int(git_config *cfg, const char *name, int *out) +int git_config_get_int32(git_config *cfg, const char *name, int32_t *out) { - long long tmp_long; - int tmp_int, ret; + int64_t tmp_long; + int32_t tmp_int; + int ret; - ret = git_config_get_long(cfg, name, &tmp_long); + ret = git_config_get_int64(cfg, name, &tmp_long); if (ret < GIT_SUCCESS) return git__rethrow(ret, "Failed to convert value for '%s'", name); @@ -284,7 +294,7 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out) } /* Try to parse it as an integer */ - error = git_config_get_int(cfg, name, out); + error = git_config_get_int32(cfg, name, out); if (error == GIT_SUCCESS) *out = !!(*out); diff --git a/src/util.c b/src/util.c index dc0eab38d..c81ed2d3a 100644 --- a/src/util.c +++ b/src/util.c @@ -46,10 +46,10 @@ int git__fnmatch(const char *pattern, const char *name, int flags) } } -int git__strtol64(long long *result, const char *nptr, const char **endptr, int base) +int git__strtol64(int64_t *result, const char *nptr, const char **endptr, int base) { const char *p; - long long n, nn; + int64_t n, nn; int c, ovfl, v, neg, ndig; p = nptr; @@ -124,10 +124,11 @@ Return: return GIT_SUCCESS; } -int git__strtol32(int *result, const char *nptr, const char **endptr, int base) +int git__strtol32(int32_t *result, const char *nptr, const char **endptr, int base) { - int tmp_int, error = GIT_SUCCESS; - long long tmp_long; + int error = GIT_SUCCESS; + int32_t tmp_int; + int64_t tmp_long; if ((error = git__strtol64(&tmp_long, nptr, endptr, base)) < GIT_SUCCESS) return error; diff --git a/src/util.h b/src/util.h index 888caf106..4de91b494 100644 --- a/src/util.h +++ b/src/util.h @@ -75,8 +75,8 @@ GIT_INLINE(void *) git__realloc(void *ptr, size_t size) extern int git__prefixcmp(const char *str, const char *prefix); extern int git__suffixcmp(const char *str, const char *suffix); -extern int git__strtol32(int *n, const char *buff, const char **end_buf, int base); -extern int git__strtol64(long long *n, const char *buff, const char **end_buf, int base); +extern int git__strtol32(int32_t *n, const char *buff, const char **end_buf, int base); +extern int git__strtol64(int64_t *n, const char *buff, const char **end_buf, int base); extern void git__hexdump(const char *buffer, size_t n); extern uint32_t git__hash(const void *key, int len, uint32_t seed); diff --git a/tests/t00-core.c b/tests/t00-core.c index 588b3c63f..703504bf8 100644 --- a/tests/t00-core.c +++ b/tests/t00-core.c @@ -556,7 +556,7 @@ BEGIN_TEST(rmdir1, "make sure non-empty dir cannot be deleted recusively") END_TEST BEGIN_TEST(strtol0, "parsing out 32 integers from a string") - int i; + int32_t i; must_pass(git__strtol32(&i, "123", NULL, 10)); must_be_true(i == 123); @@ -575,7 +575,7 @@ BEGIN_TEST(strtol0, "parsing out 32 integers from a string") END_TEST BEGIN_TEST(strtol1, "parsing out 64 integers from a string") - long long i; + int64_t i; must_pass(git__strtol64(&i, "123", NULL, 10)); must_be_true(i == 123); diff --git a/tests/t15-config.c b/tests/t15-config.c index 40c4eb9d5..3ec54feea 100644 --- a/tests/t15-config.c +++ b/tests/t15-config.c @@ -37,10 +37,10 @@ */ BEGIN_TEST(config0, "read a simple configuration") git_config *cfg; - int i; + int32_t i; must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config0")); - must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &i)); + must_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &i)); must_be_true(i == 0); must_pass(git_config_get_bool(cfg, "core.filemode", &i)); must_be_true(i == 1); @@ -134,29 +134,29 @@ END_TEST BEGIN_TEST(config5, "test number suffixes") git_config *cfg; - long long i; + int64_t i; must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config5")); - must_pass(git_config_get_long(cfg, "number.simple", &i)); + must_pass(git_config_get_int64(cfg, "number.simple", &i)); must_be_true(i == 1); - must_pass(git_config_get_long(cfg, "number.k", &i)); + must_pass(git_config_get_int64(cfg, "number.k", &i)); must_be_true(i == 1 * 1024); - must_pass(git_config_get_long(cfg, "number.kk", &i)); + must_pass(git_config_get_int64(cfg, "number.kk", &i)); must_be_true(i == 1 * 1024); - must_pass(git_config_get_long(cfg, "number.m", &i)); + must_pass(git_config_get_int64(cfg, "number.m", &i)); must_be_true(i == 1 * 1024 * 1024); - must_pass(git_config_get_long(cfg, "number.mm", &i)); + must_pass(git_config_get_int64(cfg, "number.mm", &i)); must_be_true(i == 1 * 1024 * 1024); - must_pass(git_config_get_long(cfg, "number.g", &i)); + must_pass(git_config_get_int64(cfg, "number.g", &i)); must_be_true(i == 1 * 1024 * 1024 * 1024); - must_pass(git_config_get_long(cfg, "number.gg", &i)); + must_pass(git_config_get_int64(cfg, "number.gg", &i)); must_be_true(i == 1 * 1024 * 1024 * 1024); git_config_free(cfg); @@ -195,37 +195,37 @@ END_TEST BEGIN_TEST(config9, "replace a value") git_config *cfg; int i; - long long l, expected = +9223372036854775803; + int64_t l, expected = +9223372036854775803; /* By freeing the config, we make sure we flush the values */ must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_set_int(cfg, "core.dummy", 5)); + must_pass(git_config_set_int32(cfg, "core.dummy", 5)); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_get_int(cfg, "core.dummy", &i)); + must_pass(git_config_get_int32(cfg, "core.dummy", &i)); must_be_true(i == 5); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_set_int(cfg, "core.dummy", 1)); + must_pass(git_config_set_int32(cfg, "core.dummy", 1)); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_set_long(cfg, "core.verylong", expected)); + must_pass(git_config_set_int64(cfg, "core.verylong", expected)); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_get_long(cfg, "core.verylong", &l)); + must_pass(git_config_get_int64(cfg, "core.verylong", &l)); must_be_true(l == expected); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_fail(git_config_get_int(cfg, "core.verylong", &i)); + must_fail(git_config_get_int32(cfg, "core.verylong", &i)); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_set_long(cfg, "core.verylong", 1)); + must_pass(git_config_set_int64(cfg, "core.verylong", 1)); git_config_free(cfg); END_TEST @@ -233,11 +233,11 @@ END_TEST BEGIN_TEST(config10, "a repo's config overrides the global config") git_repository *repo; git_config *cfg; - int version; + int32_t version; must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL)); - must_pass(git_config_get_int(cfg, "core.repositoryformatversion", &version)); + must_pass(git_config_get_int32(cfg, "core.repositoryformatversion", &version)); must_be_true(version == 0); git_config_free(cfg); git_repository_free(repo); @@ -246,11 +246,11 @@ END_TEST BEGIN_TEST(config11, "fall back to the global config") git_repository *repo; git_config *cfg; - int num; + int32_t num; must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_config(&cfg, repo, GLOBAL_CONFIG, NULL)); - must_pass(git_config_get_int(cfg, "core.something", &num)); + must_pass(git_config_get_int32(cfg, "core.something", &num)); must_be_true(num == 2); git_config_free(cfg); git_repository_free(repo); @@ -258,11 +258,11 @@ END_TEST BEGIN_TEST(config12, "delete a value") git_config *cfg; - int i; + int32_t i; /* By freeing the config, we make sure we flush the values */ must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_pass(git_config_set_int(cfg, "core.dummy", 5)); + must_pass(git_config_set_int32(cfg, "core.dummy", 5)); git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); @@ -270,8 +270,8 @@ BEGIN_TEST(config12, "delete a value") git_config_free(cfg); must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config9")); - must_be_true(git_config_get_int(cfg, "core.dummy", &i) == GIT_ENOTFOUND); - must_pass(git_config_set_int(cfg, "core.dummy", 1)); + must_be_true(git_config_get_int32(cfg, "core.dummy", &i) == GIT_ENOTFOUND); + must_pass(git_config_set_int32(cfg, "core.dummy", 1)); git_config_free(cfg); END_TEST @@ -294,12 +294,12 @@ END_TEST BEGIN_TEST(config15, "add a variable in an existing section") git_config *cfg; - int i; + int32_t i; /* By freeing the config, we make sure we flush the values */ must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10")); - must_pass(git_config_set_int(cfg, "empty.tmp", 5)); - must_pass(git_config_get_int(cfg, "empty.tmp", &i)); + must_pass(git_config_set_int32(cfg, "empty.tmp", 5)); + must_pass(git_config_get_int32(cfg, "empty.tmp", &i)); must_be_true(i == 5); must_pass(git_config_delete(cfg, "empty.tmp")); git_config_free(cfg); @@ -307,13 +307,13 @@ END_TEST BEGIN_TEST(config16, "add a variable in a new section") git_config *cfg; - int i; + int32_t i; git_filebuf buf; /* By freeing the config, we make sure we flush the values */ must_pass(git_config_open_ondisk(&cfg, CONFIG_BASE "/config10")); - must_pass(git_config_set_int(cfg, "section.tmp", 5)); - must_pass(git_config_get_int(cfg, "section.tmp", &i)); + must_pass(git_config_set_int32(cfg, "section.tmp", 5)); + must_pass(git_config_get_int32(cfg, "section.tmp", &i)); must_be_true(i == 5); must_pass(git_config_delete(cfg, "section.tmp")); git_config_free(cfg);