Skip to contentSkip to conversation

Smatch warning fixups

PATCH In review Lucas Stach 4 participants 18 messages Feb 7, 2014 – Feb 10, 2014

Subsystems (11)
lore
Share & tools
lore
#0 lore raw

cover Smatch warning fixups

I've run smatch over a imx_v7_defconfig build and fixed the resulting warnings.

Some of them are just minor issues with no real impact, but some of them are real potential problems or even real bugs.

Lucas Stach (14):
remove redundant NULL check on free
commands: edit: properly propagate error code
common: env: properly propagate error code
clk: imx: unsigned mfn is never less than zero
lib: math: fix return path (numstack may be NULL)
lib: libbb: f should never be NULL
comamnds: uimage: actually print error message
usb: ulpi: fix logic-op
usb: dfu: fix error path to avoid NULL ptr deref
net: usb: smsc95xx: fix wrong phy reset condition
net: usb: asix: properly propagate error code
mtd: core: avoid possible NULL ptr deref
clk: avoid possible NULL ptr deref
don't cast negative error codes to unsigned size_t

 arch/arm/mach-imx/clk-pllv1.c | 2 --
 commands/edit.c               | 2 +-
 commands/uimage.c             | 2 +-
 common/environment.c          | 5 ++---
 common/uimage.c               | 6 +++---
 drivers/clk/clkdev.c          | 8 +++++---
 drivers/eeprom/at25.c         | 3 +--
 drivers/mtd/core.c            | 3 ++-
 drivers/net/usb/asix.c        | 4 ++--
 drivers/net/usb/smsc95xx.c    | 2 +-
 drivers/usb/core/usb.c        | 3 +--
 drivers/usb/gadget/dfu.c      | 4 +++-
 drivers/usb/otg/ulpi.c        | 4 ++--
 include/image.h               | 2 +-
 lib/libbb.c                   | 2 +-
 lib/math.c                    | 4 ++--
 lib/parameter.c               | 3 +--
 net/eth.c                     | 2 +-
 18 files changed, 30 insertions(+), 31 deletions(-)
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw

PATCH 1/14 remove redundant NULL check on free

free() already checks the pointer to be non
NULL. No need to do it again.

---
 common/environment.c   | 3 +--
 drivers/eeprom/at25.c  | 3 +--
 drivers/usb/core/usb.c | 3 +--
 lib/parameter.c        | 3 +--
 net/eth.c              | 2 +-
 5 files changed, 5 insertions(+), 9 deletions(-)
common/environment.c +1 -2
index 6f06bfc..ab82103 100644
@@ -371,8 +371,7 @@ skip:
 	ret = 0;
 out:
 	close(envfd);
-	if (buf_free)
-		free(buf_free);
+	free(buf_free);
 	return ret;
 }
 
drivers/eeprom/at25.c +1 -2
index 68b4710..0a099e1 100644
@@ -294,8 +294,7 @@ static int at25_probe(struct device_d *dev)
 	return 0;
 
 fail:
-	if (at25)
-		free(at25);
+	free(at25);
 
 	return err;
 }
drivers/usb/core/usb.c +1 -2
index 68a51d1..fe1ac02 100644
@@ -504,8 +504,7 @@ int usb_host_detect(struct usb_host *host, int force)
 
 		list_del(&dev->list);
 		unregister_device(&dev->dev);
-		if (dev->hub)
-			free(dev->hub);
+		free(dev->hub);
 		dma_free(dev->setup_packet);
 		dma_free(dev->descriptor);
 		free(dev);
lib/parameter.c +1 -2
index c5c6426..a0bae3e 100644
@@ -108,8 +108,7 @@ int dev_set_param(struct device_d *dev, const char *name, const char *val)
 int dev_param_set_generic(struct device_d *dev, struct param_d *p,
 		const char *val)
 {
-	if (p->value)
-		free(p->value);
+	free(p->value);
 	if (!val) {
 		p->value = NULL;
 		return 0;
net/eth.c +1 -1
index 37dd9e0..cb59e76 100644
@@ -382,7 +382,7 @@ void eth_unregister(struct eth_device *edev)
 
 	dev_remove_parameters(&edev->dev);
 
-	if (IS_ENABLED(CONFIG_OFDEVICE) && edev->nodepath)
+	if (IS_ENABLED(CONFIG_OFDEVICE))
 		free(edev->nodepath);
 
 	unregister_device(&edev->dev);
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw

PATCH 2/14 commands: edit: properly propagate error code

In reply to Lucas Stach
---
 commands/edit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
commands/edit.c +1 -1
index 295d0a7..6b34101 100644
@@ -266,7 +266,7 @@ static int save_file(const char *path)
 	fd = open(path, O_WRONLY | O_TRUNC | O_CREAT);
 	if (fd < 0) {
 		printf("could not open file for writing: %s\n", errno_str());
-		return -1;
+		return fd;
 	}
 
 	line = buffer;
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

PATCH 3/14 common: env: properly propagate error code

In reply to Lucas Stach
---
 common/environment.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/environment.c +1 -1
index ab82103..8b22edf 100644
@@ -245,7 +245,7 @@ int envfs_load(const char *filename, char *dir, unsigned flags)
 	envfd = open(filename, O_RDONLY);
 	if (envfd < 0) {
 		printf("Open %s %s\n", filename, errno_str());
-		return -1;
+		return envfd;
 	}
 
 	/* read superblock */
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw

PATCH 4/14 clk: imx: unsigned mfn is never less than zero

In reply to Lucas Stach
---
 arch/arm/mach-imx/clk-pllv1.c | 2 --
 1 file changed, 2 deletions(-)
arch/arm/mach-imx/clk-pllv1.c +0 -2
index 6785da0..372ffb5 100644
@@ -60,8 +60,6 @@ static unsigned long clk_pllv1_recalc_rate(struct clk *clk,
 	ll = (unsigned long long)freq * mfn_abs;
 
 	do_div(ll, mfd + 1);
-	if (mfn < 0)
-		ll = -ll;
 	ll = (freq * mfi) + ll;
 
 	return ll;
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

PATCH 5/14 lib: math: fix return path (numstack may be NULL)

In reply to Lucas Stach
---
 lib/math.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/math.c +2 -2
index a4731ed..2e53b09 100644
@@ -546,8 +546,8 @@ static arith_t evaluate_string(arith_state_t *math_state, const char *expr)
 	arith_t result;
 
 	if (numstack == NULL || stack == NULL) {
-		errmsg = "out of memory";
-		goto err_with_custom_msg;
+		math_state->errmsg = "out of memory";
+		return -1;
 	}
 
 	/* Start with a left paren */
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#6 lore raw

PATCH 6/14 lib: libbb: f should never be NULL

In reply to Lucas Stach

We assume it to be non NULL in other places, so
we are in much greater trouble if it actually is.

---
 lib/libbb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/libbb.c +1 -1
index 189a170..dd42e66 100644
@@ -43,7 +43,7 @@ EXPORT_SYMBOL(concat_path_file);
 
 char *concat_subpath_file(const char *path, const char *f)
 {
-	if (f && DOT_OR_DOTDOT(f))
+	if (DOT_OR_DOTDOT(f))
 		return NULL;
 	return concat_path_file(path, f);
 }
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#7 lore raw

PATCH 7/14 comamnds: uimage: actually print error message

In reply to Lucas Stach
---
 commands/uimage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
commands/uimage.c +1 -1
index 345e496..4b0ed4d 100644
@@ -63,8 +63,8 @@ static int do_uimage(int argc, char *argv[])
 		printf("verifying data crc... ");
 		ret = uimage_verify(handle);
 		if (ret) {
-			goto err;
 			printf("Bad Data CRC\n");
+			goto err;
 		} else {
 			printf("ok\n");
 		}
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#8 lore raw

Re: [PATCH 07/14] comamnds: uimage: actually print error message

Hi Lucas,

Quoted 10 lines · Lucas Stach
---
 commands/uimage.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
commands/uimage.c +0 -0
index 345e496..4b0ed4d 100644

commands/uimage.c static int do_uimage(int argc, char *argv[]) · line 67

Quoted 6 lines
commands/uimage.c +1 -1
@@ -63,8 +63,8 @@ static int do_uimage(int argc, char *argv[])
 		printf("verifying data crc... ");
 		ret = uimage_verify(handle);
 		if (ret) {
-			goto err;
 			printf("Bad Data CRC\n");
+			goto err;

I'd drop the printf instead because in the bad crc case uimage_verify() already printfs an error. But instead let it give tongue in the other error cases (like lseek() or read() failing)

Best regards
Uwe

Signature · 7 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#9 lore raw

PATCH 8/14 usb: ulpi: fix logic-op

In reply to Lucas Stach

Checking register bits is a bitwise operation.

---
 drivers/usb/otg/ulpi.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/otg/ulpi.c +2 -2
index e0aa201..6fef5f2 100644
@@ -51,7 +51,7 @@ int ulpi_read(int reg, void __iomem *view)
 	int ret;
 
 	/* make sure interface is running */
-	if (!(readl(view) && ULPIVW_SS)) {
+	if (!(readl(view) & ULPIVW_SS)) {
 		writel(ULPIVW_WU, view);
 
 		/* wait for wakeup */
@@ -73,7 +73,7 @@ int ulpi_set(u8 bits, int reg, void __iomem *view)
 	int ret;
 
 	/* make sure the interface is running */
-	if (!(readl(view) && ULPIVW_SS)) {
+	if (!(readl(view) & ULPIVW_SS)) {
 		writel(ULPIVW_WU, view);
 		/* wait for wakeup */
 		ret = ulpi_poll(view, ULPIVW_WU);
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#10 lore raw

PATCH 9/14 usb: dfu: fix error path to avoid NULL ptr deref

In reply to Lucas Stach
---
 drivers/usb/gadget/dfu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/usb/gadget/dfu.c +3 -1
index e15fc41..4d4c347 100644
@@ -597,8 +597,10 @@ static int dfu_bind_config(struct usb_configuration *c)
 	func->disable = dfu_disable;
 
 	dfu->dnreq = usb_ep_alloc_request(c->cdev->gadget->ep0);
-	if (!dfu->dnreq)
+	if (!dfu->dnreq) {
 		printf("usb_ep_alloc_request failed\n");
+		goto out;
+	}
 	dfu->dnreq->buf = dma_alloc(CONFIG_USBD_DFU_XFER_SIZE);
 	dfu->dnreq->complete = dn_complete;
 	dfu->dnreq->zero = 0;
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#11 lore raw

PATCH 10/14 net: usb: smsc95xx: fix wrong phy reset condition

In reply to Lucas Stach
---
 drivers/net/usb/smsc95xx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/usb/smsc95xx.c +1 -1
index 38ca12f..eb8f0be 100644
@@ -447,7 +447,7 @@ static int smsc95xx_phy_initialize(struct usbnet *dev)
 		udelay(10 * 1000);
 		bmcr = smsc95xx_mdio_read(&dev->miibus, phy_id, MII_BMCR);
 		timeout++;
-	} while ((bmcr & MII_BMCR) && (timeout < 100));
+	} while ((bmcr & BMCR_RESET) && (timeout < 100));
 
 	if (timeout >= 100) {
 		netdev_warn(dev->net, "timeout on PHY Reset");
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#12 lore raw

PATCH 11/14 net: usb: asix: properly propagate error code

In reply to Lucas Stach
---
 drivers/net/usb/asix.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/usb/asix.c +2 -2
index b58db5d..37057a7 100644
@@ -382,7 +382,7 @@ static int asix_get_ethaddr(struct eth_device *edev, unsigned char *adr)
 
 	if (ret < 0) {
 		debug("Failed to read MAC address: %d\n", ret);
-		return -1;
+		return ret;
 	}
 
 	return 0;
@@ -403,7 +403,7 @@ static int ax88172_get_ethaddr(struct eth_device *edev, unsigned char *adr)
 	if ((ret = asix_read_cmd(udev, AX88172_CMD_READ_NODE_ID,
 				0, 0, 6, adr)) < 0) {
 		debug("read AX_CMD_READ_NODE_ID failed: %d\n", ret);
-		return -1;
+		return ret;
 	}
 
 	return 0;
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#13 lore raw

PATCH 12/14 mtd: core: avoid possible NULL ptr deref

In reply to Lucas Stach
---
 drivers/mtd/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/mtd/core.c +2 -1
index 6db1c6d..3439a38 100644
@@ -404,7 +404,8 @@ int add_mtd_device(struct mtd_info *mtd, char *devname, int device_id)
 	}
 
 	devfs_create(&mtd->cdev);
-	of_parse_partitions(&mtd->cdev, mtd->parent->device_node);
+	if (mtd->parent)
+		of_parse_partitions(&mtd->cdev, mtd->parent->device_node);
 
 	list_for_each_entry(hook, &mtd_register_hooks, hook)
 		if (hook->add_mtd_device)
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#14 lore raw

PATCH 13/14 clk: avoid possible NULL ptr deref

In reply to Lucas Stach
---
 drivers/clk/clkdev.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/clk/clkdev.c +5 -3
index 66cd832..d9a1c21 100644
@@ -179,9 +179,11 @@ struct clk *clk_get(struct device_d *dev, const char *con_id)
 	if (!IS_ERR(clk))
 		return clk;
 
-	clk = of_clk_get_by_name(dev->device_node, con_id);
-		if (!IS_ERR(clk))
-			return clk;
+	if (dev) {
+		clk = of_clk_get_by_name(dev->device_node, con_id);
+			if (!IS_ERR(clk))
+				return clk;
+	}
 
 	return clk_get_sys(dev_id, con_id);
 }
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#15 lore raw

PATCH 14/14 don't cast negative error codes to unsigned size_t

In reply to Lucas Stach

The cast prevents us from doing proper error checking.

---
 common/uimage.c | 6 +++---
 include/image.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
common/uimage.c +3 -3
index 7fbef86..4296359 100644
@@ -74,7 +74,7 @@ void uimage_print_contents(struct uimage_handle *handle)
 }
 EXPORT_SYMBOL(uimage_print_contents);
 
-size_t uimage_get_size(struct uimage_handle *handle, unsigned int image_no)
+int uimage_get_size(struct uimage_handle *handle, unsigned int image_no)
 {
 	if (image_no >= handle->nb_data_entries)
 		return -EINVAL;
@@ -373,7 +373,7 @@ struct resource *file_to_sdram(const char *filename, unsigned long adr)
 	struct resource *res;
 	size_t size = BUFSIZ;
 	size_t ofs = 0;
-	size_t now;
+	int now;
 	int fd;
 
 	fd = open(filename, O_RDONLY);
@@ -420,7 +420,7 @@ struct resource *uimage_load_to_sdram(struct uimage_handle *handle,
 		int image_no, unsigned long load_address)
 {
 	int ret;
-	size_t size;
+	int size;
 	resource_size_t start = (resource_size_t)load_address;
 
 	uimage_buf = (void *)load_address;
include/image.h +1 -1
index 0c8a4b1..1592348 100644
@@ -230,7 +230,7 @@ int uimage_verify(struct uimage_handle *handle);
 int uimage_load(struct uimage_handle *handle, unsigned int image_no,
 		int(*flush)(void*, unsigned int));
 void uimage_print_contents(struct uimage_handle *handle);
-size_t uimage_get_size(struct uimage_handle *handle, unsigned int image_no);
+int uimage_get_size(struct uimage_handle *handle, unsigned int image_no);
 struct resource *uimage_load_to_sdram(struct uimage_handle *handle,
 		int image_no, unsigned long load_address);
 void *uimage_load_to_buf(struct uimage_handle *handle, int image_no,
Signature · 7 lines
1.8.5.3


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#16 lore raw

Re: [PATCH 14/14] don't cast negative error codes to unsigned size_t

Quoted 13 lines · On Friday 07 February 2014 09:48:56 Lucas Stach wrote:

The cast prevents us from doing proper error checking.

---
 common/uimage.c | 6 +++---
 include/image.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
common/uimage.c +0 -0
index 7fbef86..4296359 100644

common/uimage.c void uimage_print_contents(struct uimage_handle *handle) · line 77

Quoted 5 lines
common/uimage.c +1 -1
@@ -74,7 +74,7 @@ void uimage_print_contents(struct uimage_handle *handle)
 }
 EXPORT_SYMBOL(uimage_print_contents);
 
-size_t uimage_get_size(struct uimage_handle *handle, unsigned int image_no)
+int uimage_get_size(struct uimage_handle *handle, unsigned int image_no)

What about "ssize_t"?

jbe

Signature · 7 lines
Pengutronix e.K.                              | Juergen Beisert             |
Linux Solutions for Science and Industry      | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
maintainer
#17 lore raw
In reply to Lucas Stach
Quoted 7 lines · Lucas Stach

I've run smatch over a imx_v7_defconfig build and fixed the resulting warnings.

Some of them are just minor issues with no real impact, but some of them are real potential problems or even real bugs.

Quoted 15 lines

Lucas Stach (14):
remove redundant NULL check on free
commands: edit: properly propagate error code
common: env: properly propagate error code
clk: imx: unsigned mfn is never less than zero
lib: math: fix return path (numstack may be NULL)
lib: libbb: f should never be NULL
comamnds: uimage: actually print error message
usb: ulpi: fix logic-op
usb: dfu: fix error path to avoid NULL ptr deref
net: usb: smsc95xx: fix wrong phy reset condition
net: usb: asix: properly propagate error code
mtd: core: avoid possible NULL ptr deref
clk: avoid possible NULL ptr deref
don't cast negative error codes to unsigned size_t

Applied all except 07/14 and 14/14

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox