]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include; [tt.c] check for array size in columns parser
authorKarel Zak <kzak@redhat.com>
Wed, 27 Jul 2011 14:21:11 +0000 (16:21 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 27 Jul 2011 14:21:11 +0000 (16:21 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/tt.h
lib/tt.c
misc-utils/findmnt.c
misc-utils/lsblk.c
partx/partx.c

index c3dcb2b365f4ed57d7e1dbe2807fcdc730adb3e4..5f6e8172be6ec238c4d67b4029bfa36a07f8fcf3 100644 (file)
@@ -83,7 +83,7 @@ extern struct tt_line *tt_add_line(struct tt *tb, struct tt_line *parent);
 extern int tt_line_set_data(struct tt_line *ln, int colnum, const char *data);
 extern int tt_line_set_userdata(struct tt_line *ln, void *data);
 
-extern int tt_parse_columns_list(const char *list, int cols[], int *ncols,
+extern int tt_parse_columns_list(const char *list, int ary[], size_t arrsz,
                                int (name2id)(const char *, size_t));
 
 #endif /* UTIL_LINUX_TT_H */
index 576d2b36a7b661a4964d91a6504c06f4f0d32eaf..81f7dbd09ef3a01c69bef59119b280afd272ef00 100644 (file)
--- a/lib/tt.c
+++ b/lib/tt.c
@@ -661,16 +661,19 @@ int tt_print_table(struct tt *tb)
        return 0;
 }
 
-int tt_parse_columns_list(const char *list, int cols[], int *ncols,
+/* Returns: >= 0  : number of items added to ary[]
+ *            -1  : parse error or unknown item
+ *            -2  : arysz reached
+ */
+int tt_parse_columns_list(const char *list, int ary[], size_t arysz,
                        int (name2id)(const char *, size_t))
 {
        const char *begin = NULL, *p;
+       int n = 0;
 
-       if (!list || !*list || !cols || !ncols || !name2id)
+       if (!list || !*list || !ary || !arysz || !name2id)
                return -1;
 
-       *ncols = 0;
-
        for (p = list; p && *p; p++) {
                const char *end = NULL;
                int id;
@@ -689,13 +692,14 @@ int tt_parse_columns_list(const char *list, int cols[], int *ncols,
                id = name2id(begin, end - begin);
                if (id == -1)
                        return -1;
-               cols[ *ncols ] = id;
-               (*ncols)++;
+               ary[ n++ ] = id;
+               if (n >= arysz)
+                       return -2;
                begin = NULL;
                if (end && !*end)
                        break;
        }
-       return 0;
+       return n;
 }
 
 #ifdef TEST_PROGRAM
index 86f3fd68716c762af487e044d82c0196ec415679..2dbb60e5ab26a1415a5cba45ffe642eeb7f9efa1 100644 (file)
@@ -897,19 +897,25 @@ int main(int argc, char *argv[])
                        disable_columns_truncate();
                        break;
                case 'o':
-                       if (tt_parse_columns_list(optarg, columns, &ncolumns,
-                                               column_name_to_id))
+                       ncolumns = tt_parse_columns_list(
+                                               optarg,
+                                               columns, ARRAY_SIZE(columns),
+                                               column_name_to_id);
+                       if (ncolumns < 0)
                                exit(EXIT_FAILURE);
                        break;
                case 'O':
                        set_match(COL_OPTIONS, optarg);
                        break;
                case 'p':
-                       if (optarg &&
-                           tt_parse_columns_list(optarg, actions, &nactions,
-                                               poll_action_name_to_id))
-                               exit(EXIT_FAILURE);
-
+                       if (optarg) {
+                               nactions = tt_parse_columns_list(
+                                               optarg,
+                                               actions, ARRAY_SIZE(actions),
+                                               poll_action_name_to_id);
+                               if (nactions < 0)
+                                       exit(EXIT_FAILURE);
+                       }
                        flags |= FL_POLL;
                        tt_flags &= ~TT_FL_TREE;
                        break;
index 2e2c457dfd2c5e22e1977e575525abf6dc84093d..b458486f0acfc9dca18b433443c5e8f0e8f2ccce 100644 (file)
@@ -971,8 +971,11 @@ int main(int argc, char *argv[])
                        tt_flags |= TT_FL_NOHEADINGS;
                        break;
                case 'o':
-                       if (tt_parse_columns_list(optarg, columns, &ncolumns,
-                                               column_name_to_id))
+                       ncolumns = tt_parse_columns_list(
+                                               optarg,
+                                               columns, ARRAY_SIZE(columns),
+                                               column_name_to_id);
+                       if (ncolumns < 0)
                                return EXIT_FAILURE;
                        break;
                case 'P':
index 4f827a822289748a9d05977c22b07f7824dd4690..2451993e41343a2891f31d7d7c945aee4ee79839 100644 (file)
@@ -676,8 +676,11 @@ int main(int argc, char **argv)
                                errx(EXIT_FAILURE, _("failed to parse --nr <M-N> range"));
                        break;
                case 'o':
-                       if (tt_parse_columns_list(optarg, columns, &ncolumns,
-                                               column_name_to_id))
+                       ncolumns = tt_parse_columns_list(
+                                               optarg,
+                                               columns, ARRAY_SIZE(columns),
+                                               column_name_to_id);
+                       if (ncolumns < 0)
                                return EXIT_FAILURE;
                        break;
                case 'P':