-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2-0001-Btrfs-progs-new-helper-to-parse-string-to-u64-for.patch
68 lines (62 loc) · 2.04 KB
/
v2-0001-Btrfs-progs-new-helper-to-parse-string-to-u64-for.patch
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
From 848efb62a76d1f16462d626a9bc11fb272185de8 Mon Sep 17 00:00:00 2001
From: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
Date: Wed, 19 Feb 2014 19:17:51 +0800
Subject: [PATCH v2 1/4] Btrfs-progs: new helper to parse string to u64 for
btrfs
There are many places that need parse string to u64 for btrfs commands,
in fact, we do such things *too casually*, using atoi/atol/atoll..is not
right at all, and even we don't check whether it is a valid string.
Let's do everything more gracefully, we introduce a new helper
btrfs_strtoull() which will do all the necessary checks.If we fail to
parse string to u64, we will output message and exit directly, this is
something like what usage() is doing. It is ok to not return erro to
it's caller, because this function should be called when parsing arg
(just like usage!)
Signed-off-by: Wang Shilong <wangsl.fnst@cn.fujitsu.com>
---
utils.c | 19 +++++++++++++++++++
utils.h | 1 +
2 files changed, 20 insertions(+)
diff --git a/utils.c b/utils.c
index 97e23d5..9d33407 100644
--- a/utils.c
+++ b/utils.c
@@ -1520,6 +1520,25 @@ scan_again:
return 0;
}
+u64 btrfs_strtoull(char *str)
+{
+ u64 value;
+ char *ptr_parse_end = NULL;
+ char *ptr_str_end = str + strlen(str);
+
+ value = strtoull(str, &ptr_parse_end, 0);
+ if (ptr_parse_end != ptr_str_end) {
+ fprintf(stderr, "ERROR: %s is an invalid unsigned long long integer.\n",
+ str);
+ exit(1);
+ }
+ if (value == ULLONG_MAX) {
+ fprintf(stderr, "ERROR: %s is out of range.\n", str);
+ exit(1);
+ }
+ return value;
+}
+
u64 parse_size(char *s)
{
int i;
diff --git a/utils.h b/utils.h
index 04b8c45..e0cc470 100644
--- a/utils.h
+++ b/utils.h
@@ -71,6 +71,7 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_bytes);
int get_mountpt(char *dev, char *mntpt, size_t size);
int btrfs_scan_block_devices(int run_ioctl);
u64 parse_size(char *s);
+u64 btrfs_strtoull(char *str);
int open_file_or_dir(const char *fname, DIR **dirstream);
void close_file_or_dir(int fd, DIR *dirstream);
int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
--
1.8.4