The function uses "int" as argument, but for array size (and index) is better
to use unsigned type (size_t). If we mix "size_t" in util (e.g. fdisk)
and "int" in lib/strutils.c then result is unexpected behavior on
ppc64.
# sfdisk --list -o DEVICE,START,SIZE /dev/sdb
Disk /dev/sdb: 50 MiB,
52428800 bytes, 102400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 32768 bytes
Disklabel type: gpt
Disk identifier:
3B8559DB-33AF-43E9-BEFC-
C331D829B539
lt-sfdisk: libfdisk/src/label.c:178: fdisk_label_get_field: Assertion `id > 0' failed.
The patch cleanup all code to use size_t everywhere.
Signed-off-by: Karel Zak <kzak@redhat.com>
/* extend or replace fields_nids[] according to fields_string */
if (fields_string &&
string_add_to_idarray(fields_string, fields_ids, FDISK_NFIELDS * 2,
- (int *) &fields_nids, fieldname_to_id) < 0)
+ &fields_nids, fieldname_to_id) < 0)
exit(EXIT_FAILURE);
done:
fields_label = NULL;
#define NCOLS ARRAY_SIZE(infos)
/* array with IDs of enabled columns */
-static int columns[NCOLS], ncolumns;
+static int columns[NCOLS];
+size_t ncolumns;
static int verbose;
static int partx_flags;
extern int string_to_idarray(const char *list, int ary[], size_t arysz,
int (name2id)(const char *, size_t));
extern int string_add_to_idarray(const char *list, int ary[],
- size_t arysz, int *ary_pos,
+ size_t arysz, size_t *ary_pos,
int (name2id)(const char *, size_t));
extern int string_to_bitarray(const char *list, char *ary,
* it adds fields to array instead of replacing them.
*/
int string_add_to_idarray(const char *list, int ary[], size_t arysz,
- int *ary_pos, int (name2id)(const char *, size_t))
+ size_t *ary_pos, int (name2id)(const char *, size_t))
{
const char *list_add;
int r;
- if (!list || !*list || !ary_pos ||
- *ary_pos < 0 || (size_t) *ary_pos > arysz)
+ if (!list || !*list || !ary_pos || *ary_pos > arysz)
return -1;
if (list[0] == '+')
* column twice. That's enough, dynamically allocated array of the columns is
* unnecessary overkill and over-engineering in this case */
static int columns[ARRAY_SIZE(infos) * 2];
-static int ncolumns;
+static size_t ncolumns;
static inline size_t err_columns_index(size_t arysz, size_t idx)
{
static int get_column_id(int num)
{
- assert(num < ncolumns);
+ assert(num >= 0);
+ assert((size_t) num < ncolumns);
assert((size_t) columns[num] < ARRAY_SIZE(infos));
return columns[num];
}
static struct libscols_line *add_line(struct libscols_table *table, struct libmnt_fs *fs,
struct libscols_line *parent)
{
- int i;
+ size_t i;
struct libscols_line *line = scols_table_new_line(table, parent);
if (!line) {
static struct libscols_line *add_tabdiff_line(struct libscols_table *table, struct libmnt_fs *new_fs,
struct libmnt_fs *old_fs, int change)
{
- int i;
+ size_t i;
struct libscols_line *line = scols_table_new_line(table, NULL);
if (!line) {
struct libmnt_table *tb = NULL;
char **tabfiles = NULL;
int direction = MNT_ITER_FORWARD;
- int i, c, rc = -1, timeout = -1;
+ int c, rc = -1, timeout = -1;
int ntabfiles = 0, tabtype = 0;
char *outarg = NULL;
+ size_t i;
struct libscols_table *table = NULL;
* column twice. That's enough, dynamically allocated array of the columns is
* unnecessary overkill and over-engineering in this case */
static int columns[ARRAY_SIZE(infos) * 2];
-static int ncolumns;
+static size_t ncolumns;
static inline size_t err_columns_index(size_t arysz, size_t idx)
{
/* array with IDs of enabled columns */
static int get_column_id(int num)
{
- assert(num < ncolumns);
+ assert(num >= 0);
+ assert((size_t) num < ncolumns);
assert(columns[num] < (int) ARRAY_SIZE(infos));
return columns[num];
}
{
size_t i;
- for (i = 0; i < (size_t) ncolumns; i++)
+ for (i = 0; i < ncolumns; i++)
if (columns[i] == id)
return i;
return -1;
static void fill_table_line(struct blkdev_cxt *cxt, struct libscols_line *scols_parent)
{
- int i;
+ size_t i;
cxt->scols_line = scols_table_new_line(lsblk->table, scols_parent);
if (!cxt->scols_line)
{
struct lsblk _ls = { .sort_id = -1 };
int scols_flags = LSBLK_TREE;
- int i, c, status = EXIT_FAILURE;
+ int c, status = EXIT_FAILURE;
char *outarg = NULL;
+ size_t i;
static const struct option longopts[] = {
{ "all", 0, 0, 'a' },
outarg = optarg;
break;
case 'O':
- for (ncolumns = 0 ; ncolumns < (int) ARRAY_SIZE(infos); ncolumns++)
+ for (ncolumns = 0 ; ncolumns < ARRAY_SIZE(infos); ncolumns++)
columns[ncolumns] = ncolumns;
break;
case 'p':
};
static int columns[ARRAY_SIZE(infos) * 2];
-static int ncolumns;
+static size_t ncolumns;
static pid_t pid = 0;
static inline int get_column_id(int num)
{
- assert(num < ncolumns);
+ assert(num >= 0);
+ assert((size_t) num < ncolumns);
assert(columns[num] < (int) ARRAY_SIZE(infos));
return columns[num];
static void add_scols_line(struct libscols_table *table, struct lock *l, struct list_head *locks)
{
- int i;
+ size_t i;
struct libscols_line *line;
/*
* Whenever cmdname or filename is NULL it is most
static int show_locks(struct list_head *locks)
{
- int i, rc = 0;
+ int rc = 0;
+ size_t i;
struct list_head *p, *pnext;
struct libscols_table *table;
};
static int columns[ARRAY_SIZE(infos) * 2] = {-1};
-static int ncolumns;
+static size_t ncolumns;
static int get_column_id(int num)
{
- assert(num < ncolumns);
+ assert(num >= 0);
+ assert((size_t) num < ncolumns);
assert(columns[num] < (int) ARRAY_SIZE(infos));
return columns[num];
}
static int set_scols_data(struct loopdev_cxt *lc, struct libscols_line *ln)
{
- int i;
+ size_t i;
for (i = 0; i < ncolumns; i++) {
const char *p = NULL; /* external data */
struct stat sbuf, *st = &sbuf;
struct libscols_table *tb;
struct libscols_line *ln;
- int i, rc = 0;
+ int rc = 0;
+ size_t i;
scols_init_debug(0);