Commit a70b3c7386aab50a3607731a0db1e69d01e11c73

Vicent Martí 2011-09-30T13:52:01

Merge pull request #436 from libgit2/config-int-types config: Proper type declarations for 64 bit ints

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
diff --git a/include/git2/config.h b/include/git2/config.h
index bafac68..3664759 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 54727c0..f53afa1 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 'k':
-	case 'K':
+	case 'g':
+	case 'G':
 		num *= 1024;
-		break;
+		/* fallthrough */
+
 	case 'm':
 	case 'M':
-		num *= 1024 * 1024;
-		break;
-	case 'g':
-	case 'G':
-		num *= 1024 * 1024 * 1024;
-		break;
-	default:
-		return git__throw(GIT_EINVALIDTYPE, "Failed to get value for '%s'. Value is of invalid type", name);
-	}
+		num *= 1024;
+		/* fallthrough */
 
-	*out = num;
+	case 'k':
+	case 'K':
+		num *= 1024;
 
-	return GIT_SUCCESS;
+		/* 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);
+	}
 }
 
-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 dc0eab3..c81ed2d 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 888caf1..4de91b4 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 588b3c6..703504b 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 40c4eb9..3ec54fe 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);