Add getting and setting of long int variables long int is a safer type than int unless the user knows that the variable is going to be quite small. The code has been reworked to use strtol instead of the more complicated sscanf. Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
diff --git a/src/config.c b/src/config.c
index 92d8115..78a31ae 100644
--- a/src/config.c
+++ b/src/config.c
@@ -311,17 +311,17 @@ static int config_set(git_config *cfg, const char *name, const char *value)
return error;
}
-int git_config_set_int(git_config *cfg, const char *name, int value)
+int git_config_set_long(git_config *cfg, const char *name, long int value)
{
char str_value[5]; /* Most numbers should fit in here */
int buf_len = sizeof(str_value), ret;
char *help_buf = NULL;
- if ((ret = snprintf(str_value, buf_len, "%d", value)) >= buf_len - 1){
+ if ((ret = snprintf(str_value, buf_len, "%ld", value)) >= buf_len - 1){
/* The number is too large, we need to allocate more memory */
buf_len = ret + 1;
help_buf = git__malloc(buf_len);
- snprintf(help_buf, buf_len, "%d", value);
+ snprintf(help_buf, buf_len, "%ld", value);
ret = config_set(cfg, name, help_buf);
free(help_buf);
} else {
@@ -331,6 +331,11 @@ int git_config_set_int(git_config *cfg, const char *name, int value)
return ret;
}
+int git_config_set_int(git_config *cfg, const char *name, int value)
+{
+ return git_config_set_long(cfg, name, value);
+}
+
int git_config_set_bool(git_config *cfg, const char *name, int value)
{
const char *str_value;
@@ -370,28 +375,55 @@ static int config_get(git_config *cfg, const char *name, const char **out)
return error;
}
-int git_config_get_int(git_config *cfg, const char *name, int *out)
+int git_config_get_long(git_config *cfg, const char *name, long int *out)
{
const char *value;
+ char *num_end;
int ret;
+ long int num;
ret = config_get(cfg, name, &value);
if (ret < GIT_SUCCESS)
return ret;
- ret = sscanf(value, "%d", out);
- if (ret == 0) /* No items were matched i.e. value isn't a number */
+ errno = 0;
+ num = strtol(value, &num_end, 0);
+
+ /* There was some error */
+ if (num_end == value || errno != 0)
+ return GIT_EINVALIDTYPE;
+
+ switch (*num_end) {
+ case 'k':
+ num *= 1024;
+ break;
+ case 'm':
+ num *= 1024 * 1024;
+ break;
+ case 'g':
+ num *= 1024 * 1024 * 1024;
+ break;
+ default:
return GIT_EINVALIDTYPE;
- if (ret < 0) {
- if (errno == EINVAL) /* Format was NULL */
- return GIT_EINVALIDTYPE;
- else
- return GIT_EOSERR;
}
+ *out = num;
+
return GIT_SUCCESS;
}
+int git_config_get_int(git_config *cfg, const char *name, int *out)
+{
+ long int tmp;
+ int ret;
+
+ ret = git_config_get_long(cfg, name, &tmp);
+
+ *out = (int) tmp;
+
+ return ret;
+}
+
int git_config_get_bool(git_config *cfg, const char *name, int *out)
{
const char *value;