]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: cleanup arguments checks
authorKarel Zak <kzak@redhat.com>
Tue, 4 Dec 2012 11:12:54 +0000 (12:12 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 4 Dec 2012 11:12:54 +0000 (12:12 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/cache.c
libblkid/src/dev.c
libblkid/src/devname.c
libblkid/src/encode.c
libblkid/src/partitions/partitions.c
libblkid/src/probe.c
libblkid/src/resolve.c
libblkid/src/superblocks/superblocks.c
libblkid/src/tag.c
libblkid/src/topology/topology.c
libblkid/src/verify.c

index 2592d6fe7692be97cb23999da49538a4d1505f2a..cc2cf8e944263482178cb4f04a3232fc56c78477 100644 (file)
@@ -141,6 +141,9 @@ int blkid_get_cache(blkid_cache *ret_cache, const char *filename)
 {
        blkid_cache cache;
 
+       if (!ret_cache)
+               return -BLKID_ERR_PARAM;
+
        blkid_init_debug(0);
 
        DBG(DEBUG_CACHE, printf("creating blkid cache (using %s)\n",
index 58ee6f864ddcb2d1eca93341672b665146bc324b..62dfc24d3499a0de7078b46d8489cc47bc29ac4f 100644 (file)
@@ -69,7 +69,7 @@ void blkid_free_dev(blkid_dev dev)
  */
 extern const char *blkid_dev_devname(blkid_dev dev)
 {
-       return dev->bid_name;
+       return dev ? dev->bid_name : NULL;
 }
 
 #ifdef CONFIG_BLKID_DEBUG
@@ -127,7 +127,7 @@ struct blkid_struct_dev_iterate {
 
 extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
 {
-       blkid_dev_iterate       iter;
+       blkid_dev_iterate iter;
 
        iter = malloc(sizeof(struct blkid_struct_dev_iterate));
        if (iter) {
@@ -137,7 +137,7 @@ extern blkid_dev_iterate blkid_dev_iterate_begin(blkid_cache cache)
                iter->search_type = 0;
                iter->search_value = 0;
        }
-       return (iter);
+       return iter;
 }
 
 extern int blkid_dev_set_search(blkid_dev_iterate iter,
@@ -172,9 +172,9 @@ extern int blkid_dev_next(blkid_dev_iterate iter,
 {
        blkid_dev               dev;
 
-       *ret_dev = 0;
-       if (!iter || iter->magic != DEV_ITERATE_MAGIC)
+       if  (!ret_dev || !iter || iter->magic != DEV_ITERATE_MAGIC)
                return -1;
+       *ret_dev = 0;
        while (iter->p != &iter->cache->bic_devs) {
                dev = list_entry(iter->p, struct blkid_struct_dev, bid_devs);
                iter->p = iter->p->next;
index 95f1d7ec9b622fb8ff2b064a71bb7364cebf8834..b27b6612c6e30f2e8fa03b7753a269c981cec9a4 100644 (file)
@@ -199,7 +199,7 @@ static void probe_one(blkid_cache cache, const char *ptname,
                struct stat st;
                char device[256];
 
-               sprintf(device, "%s/%s", *dir, ptname);
+               snprintf(device, sizeof(device), "%s/%s", *dir, ptname);
                if ((dev = blkid_get_dev(cache, device, BLKID_DEV_FIND)) &&
                    dev->bid_devno == devno)
                        goto set_pri;
@@ -595,9 +595,11 @@ int blkid_probe_all(blkid_cache cache)
 
        DBG(DEBUG_PROBE, printf("Begin blkid_probe_all()\n"));
        ret = probe_all(cache, 0);
-       cache->bic_time = time(0);
-       cache->bic_flags |= BLKID_BIC_FL_PROBED;
-       DBG(DEBUG_PROBE, printf("End blkid_probe_all()\n"));
+       if (ret == 0) {
+               cache->bic_time = time(0);
+               cache->bic_flags |= BLKID_BIC_FL_PROBED;
+       }
+       DBG(DEBUG_PROBE, printf("End blkid_probe_all() [rc=%d]\n", ret));
        return ret;
 }
 
@@ -615,7 +617,7 @@ int blkid_probe_all_new(blkid_cache cache)
 
        DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_new()\n"));
        ret = probe_all(cache, 1);
-       DBG(DEBUG_PROBE, printf("End blkid_probe_all_new()\n"));
+       DBG(DEBUG_PROBE, printf("End blkid_probe_all_new() [rc=%d]\n", ret));
        return ret;
 }
 
@@ -643,7 +645,7 @@ int blkid_probe_all_removable(blkid_cache cache)
 
        DBG(DEBUG_PROBE, printf("Begin blkid_probe_all_removable()\n"));
        ret = probe_all_removable(cache);
-       DBG(DEBUG_PROBE, printf("End blkid_probe_all_removable()\n"));
+       DBG(DEBUG_PROBE, printf("End blkid_probe_all_removable() [rc=%d]\n", ret));
        return ret;
 }
 
index 9a0570e8f21817a1588b80e65326a0f81cf28643..ff57be4cb3b080b4da1a5542dcdd46d0f7d7ff8a 100644 (file)
@@ -284,7 +284,7 @@ int blkid_encode_string(const char *str, char *str_enc, size_t len)
 {
        size_t i, j;
 
-       if (str == NULL || str_enc == NULL)
+       if (!str || !str_enc || !len)
                return -1;
 
        for (i = 0, j = 0; str[i] != '\0'; i++) {
@@ -332,6 +332,8 @@ err:
  */
 int blkid_safe_string(const char *str, char *str_safe, size_t len)
 {
+       if (!str || !str_safe || !len)
+               return -1;
        replace_whitespace(str, str_safe, len);
        replace_chars(str_safe, UDEV_ALLOWED_CHARS_INPUT);
        return 0;
index c9f7ff6678a858112853f0b74cf2929daafe9db8..d51d4074417967d879c932bda28ac05523a9375d 100644 (file)
@@ -228,7 +228,6 @@ int blkid_probe_set_partitions_flags(blkid_probe pr, int flags)
 {
        if (!pr)
                return -1;
-
        pr->chains[BLKID_CHAIN_PARTS].flags = flags;
        return 0;
 }
@@ -921,6 +920,9 @@ blkid_partition blkid_partlist_devno_to_partition(blkid_partlist ls, dev_t devno
        uint64_t start, size;
        int i, rc, partno = 0;
 
+       if (!ls)
+               return NULL;
+
        DBG(DEBUG_LOWPROBE,
                printf("triyng to convert devno 0x%llx to partition\n",
                        (long long) devno));
@@ -1289,7 +1291,7 @@ blkid_loff_t blkid_partition_get_size(blkid_partition par)
  */
 int blkid_partition_get_type(blkid_partition par)
 {
-       return par ? par->type : 0;
+       return par->type;
 }
 
 /* Sets partition 'type' for PT where the type is defined by string rather
@@ -1350,6 +1352,6 @@ int blkid_partition_set_flags(blkid_partition par, unsigned long long flags)
  */
 unsigned long long blkid_partition_get_flags(blkid_partition par)
 {
-       return par ? par->flags : 0;
+       return par->flags;
 }
 
index c371656a4972b1ebc4ad7ae1558282538baf6fa5..eabcf9010ba2f94887f20db3f7556e187248a9fd 100644 (file)
@@ -461,6 +461,9 @@ int __blkid_probe_invert_filter(blkid_probe pr, int chain)
        size_t i;
        struct blkid_chain *chn;
 
+       if (!pr)
+               return -1;
+
        chn = &pr->chains[chain];
 
        if (!chn->driver->has_fltr || !chn->fltr)
@@ -1397,7 +1400,7 @@ blkid_loff_t blkid_probe_get_offset(blkid_probe pr)
  * blkid_probe_get_fd:
  * @pr: probe
  *
- * Returns: file descriptor for assigned device/file.
+ * Returns: file descriptor for assigned device/file or -1 in case of error.
  */
 int blkid_probe_get_fd(blkid_probe pr)
 {
@@ -1523,7 +1526,7 @@ int blkid_probe_has_value(blkid_probe pr, const char *name)
 
 struct blkid_prval *__blkid_probe_get_value(blkid_probe pr, int num)
 {
-       if (pr == NULL || num < 0 || num >= pr->nvals)
+       if (!pr || num < 0 || num >= pr->nvals)
                return NULL;
 
        return &pr->vals[num];
@@ -1533,7 +1536,7 @@ struct blkid_prval *__blkid_probe_lookup_value(blkid_probe pr, const char *name)
 {
        int i;
 
-       if (pr == NULL || pr->nvals == 0 || name == NULL)
+       if (!pr || !pr->nvals || !name)
                return NULL;
 
        for (i = 0; i < pr->nvals; i++) {
index e5c4b5876fdeb99305ae5293bb5d5efa8f40411d..96749b3ecdf6f95addbc7d8b1d661076f39285de 100644 (file)
@@ -36,11 +36,8 @@ char *blkid_get_tag_value(blkid_cache cache, const char *tagname,
 
        if (!devname)
                return NULL;
-
-       if (!cache) {
-               if (blkid_get_cache(&c, NULL) < 0)
-                       return NULL;
-       }
+       if (!cache && blkid_get_cache(&c, NULL) < 0)
+               return NULL;
 
        if ((dev = blkid_get_dev(c, devname, BLKID_DEV_NORMAL)) &&
            (found = blkid_find_tag_dev(dev, tagname)))
@@ -68,11 +65,8 @@ char *blkid_get_devname(blkid_cache cache, const char *token,
 
        if (!token)
                return NULL;
-
-       if (!cache) {
-               if (blkid_get_cache(&c, NULL) < 0)
-                       return NULL;
-       }
+       if (!cache && blkid_get_cache(&c, NULL) < 0)
+               return NULL;
 
        DBG(DEBUG_RESOLVE,
            printf("looking for %s%s%s %s\n", token, value ? "=" : "",
index 879a40ff59506adc59459e204b0a8c9f8f149c8d..2929a5f2c8dd89871e5fbe831425c07dbca4631c 100644 (file)
@@ -255,9 +255,6 @@ int blkid_probe_filter_superblocks_usage(blkid_probe pr, int flag, int usage)
        struct blkid_chain *chn;
        size_t i;
 
-       if (!pr)
-               return -1;
-
        fltr = blkid_probe_get_filter(pr, BLKID_CHAIN_SUBLKS, TRUE);
        if (!fltr)
                return -1;
index 8fe3336342021ba8262613c355cc6ba8c0620334..9dbacef04dbd2f12e182b62cb9975a623a3a06ee 100644 (file)
@@ -86,9 +86,6 @@ extern int blkid_dev_has_tag(blkid_dev dev, const char *type,
 {
        blkid_tag               tag;
 
-       if (!dev || !type)
-               return -1;
-
        tag = blkid_find_tag_dev(dev, type);
        if (!value)
                return (tag != NULL);
@@ -242,12 +239,15 @@ int blkid_parse_tag_string(const char *token, char **ret_type, char **ret_val)
                        goto errout; /* missing closing quote */
                *cp = '\0';
        }
-       value = strdup(value);
+       if (value && *value)
+               value = strdup(value);
        if (!value)
                goto errout;
 
-       *ret_type = name;
-       *ret_val = value;
+       if (ret_type)
+               *ret_type = name;
+       if (ret_val)
+               *ret_val = value;
 
        return 0;
 
@@ -300,11 +300,13 @@ extern int blkid_tag_next(blkid_tag_iterate iter,
 {
        blkid_tag tag;
 
-       *type = 0;
-       *value = 0;
-       if (!iter || iter->magic != TAG_ITERATE_MAGIC ||
+       if (!type || !value ||
+           !iter || iter->magic != TAG_ITERATE_MAGIC ||
            iter->p == &iter->dev->bid_tags)
                return -1;
+
+       *type = 0;
+       *value = 0;
        tag = list_entry(iter->p, struct blkid_struct_tag, bit_tags);
        *type = tag->bit_name;
        *value = tag->bit_val;
index 73a397afd9edebd9c0335938cd51c359e76afa36..9d4e7bdf18d4eee65e5aefa4a429cca0a82b1683 100644 (file)
@@ -317,7 +317,7 @@ int blkid_topology_set_physical_sector_size(blkid_probe pr, unsigned long val)
  */
 unsigned long blkid_topology_get_alignment_offset(blkid_topology tp)
 {
-       return tp ? tp->alignment_offset : 0;
+       return tp->alignment_offset;
 }
 
 /**
@@ -328,7 +328,7 @@ unsigned long blkid_topology_get_alignment_offset(blkid_topology tp)
  */
 unsigned long blkid_topology_get_minimum_io_size(blkid_topology tp)
 {
-       return tp ? tp->minimum_io_size : 0;
+       return tp->minimum_io_size;
 }
 
 /**
@@ -339,7 +339,7 @@ unsigned long blkid_topology_get_minimum_io_size(blkid_topology tp)
  */
 unsigned long blkid_topology_get_optimal_io_size(blkid_topology tp)
 {
-       return tp ? tp->optimal_io_size : 0;
+       return tp->optimal_io_size;
 }
 
 /**
@@ -350,7 +350,7 @@ unsigned long blkid_topology_get_optimal_io_size(blkid_topology tp)
  */
 unsigned long blkid_topology_get_logical_sector_size(blkid_topology tp)
 {
-       return tp ? tp->logical_sector_size : 0;
+       return tp->logical_sector_size;
 }
 
 /**
@@ -361,6 +361,6 @@ unsigned long blkid_topology_get_logical_sector_size(blkid_topology tp)
  */
 unsigned long blkid_topology_get_physical_sector_size(blkid_topology tp)
 {
-       return tp ? tp->physical_sector_size : 0;
+       return tp->physical_sector_size;
 }
 
index 207152cd870057fcba656db7f44cba761cac83d8..da78d6b0832c687a8fd523ff70549b102884d4ac 100644 (file)
@@ -81,7 +81,7 @@ blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
        char *fltr[2];
        int fd;
 
-       if (!dev)
+       if (!dev || !cache)
                return NULL;
 
        now = time(0);