Skip to contentSkip to conversation

net/mlx5: fpga: avoid uninitialized return codes

PATCH In review Arnd Bergmann 3 participants 3 messages Sep 14, 2017

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

PATCH 1/1 net/mlx5: fpga: avoid uninitialized return codes

calling mlx5_fpga_mem_{read,write}_i2c() with a zero length on older compiler version such as gcc-4.6 results in a warning that the return code is not initialized:

drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c:147:6: error: ‘err’ may be used uninitialized in this function [-Werror=uninitialized] drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c:126:6: error: ‘err’ may be used uninitialized in this function [-Werror=uninitialized]

On newer compilers, the 'err' variable is optimized away in this code path and assumed to be zero when the loop completes, so we don't get this warning.

I'm changing the function here to instead return -EINVAL for the case, under the assumption that it was never meant to be called with a zero length argument.

---
 drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +2 -2
index 3c11d6e2160a..914fb9d77a1a 100644
@@ -64,7 +64,7 @@ static int mlx5_fpga_mem_read_i2c(struct mlx5_fpga_device *fdev, size_t size,
 	size_t max_size = MLX5_FPGA_ACCESS_REG_SIZE_MAX;
 	size_t bytes_done = 0;
 	u8 actual_size;
-	int err;
+	int err = -EINVAL;
 
 	if (!fdev->mdev)
 		return -ENOTCONN;
@@ -93,7 +93,7 @@ static int mlx5_fpga_mem_write_i2c(struct mlx5_fpga_device *fdev, size_t size,
 	size_t max_size = MLX5_FPGA_ACCESS_REG_SIZE_MAX;
 	size_t bytes_done = 0;
 	u8 actual_size;
-	int err;
+	int err = -EINVAL;
 
 	if (!fdev->mdev)
 		return -ENOTCONN;
Signature · 6 lines
2.9.0

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw
Quoted 7 lines · Arnd Bergmann

calling mlx5_fpga_mem_{read,write}_i2c() with a zero length on older compiler version such as gcc-4.6 results in a warning that the return code is not initialized:

drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c:147:6: error: ‘err’ may be used uninitialized in this function [-Werror=uninitialized] drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c:126:6: error: ‘err’ may be used uninitialized in this function [-Werror=uninitialized]

Quoted 7 lines

On newer compilers, the 'err' variable is optimized away in this code path and assumed to be zero when the loop completes, so we don't get this warning.

I'm changing the function here to instead return -EINVAL for the case, under the assumption that it was never meant to be called with a zero length argument.

I agree with you that size can't be zero and this patch will fix the warning, but if it is possible, I will prefer to have this check is written explicitly and not implicitly.

drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +3 -0
index 3c11d6e2160a..0e85bebe1dad 100644
@@ -69,6 +69,9 @@ static int mlx5_fpga_mem_read_i2c(struct mlx5_fpga_device *fdev, size_t size,
 	if (!fdev->mdev)
 		return -ENOTCONN;
 
+	if (!size)
+		return -EINVAL;
+
 	while (bytes_done < size) {
 		actual_size = min(max_size, (size - bytes_done));
 

Thanks

Quoted 36 lines
---
 drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +2 -3
index 3c11d6e2160a..914fb9d77a1a 100644
@@ -64,7 +64,7 @@ static int mlx5_fpga_mem_read_i2c(struct mlx5_fpga_device *fdev, size_t size,
 	size_t max_size = MLX5_FPGA_ACCESS_REG_SIZE_MAX;
 	size_t bytes_done = 0;
 	u8 actual_size;
-	int err;
+	int err = -EINVAL;
 
 	if (!fdev->mdev)
 		return -ENOTCONN;
@@ -93,7 +93,7 @@ static int mlx5_fpga_mem_write_i2c(struct mlx5_fpga_device *fdev, size_t size,
 	size_t max_size = MLX5_FPGA_ACCESS_REG_SIZE_MAX;
 	size_t bytes_done = 0;
 	u8 actual_size;
-	int err;
+	int err = -EINVAL;
 
 	if (!fdev->mdev)
 		return -ENOTCONN;
--

2.9.0

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 8 lines · Leon Romanovsky
Quoted 7 lines · Arnd Bergmann

calling mlx5_fpga_mem_{read,write}_i2c() with a zero length on older compiler version such as gcc-4.6 results in a warning that the return code is not initialized:

drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c:147:6: error: ‘err’ may be used uninitialized in this function [-Werror=uninitialized] drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c:126:6: error: ‘err’ may be used uninitialized in this function [-Werror=uninitialized]

Quoted 7 lines
Quoted 7 lines

On newer compilers, the 'err' variable is optimized away in this code path and assumed to be zero when the loop completes, so we don't get this warning.

I'm changing the function here to instead return -EINVAL for the case, under the assumption that it was never meant to be called with a zero length argument.

Thanks Arnd, Looks good.

Quoted 10 lines

I agree with you that size can't be zero and this patch will fix the warning, but if it is possible, I will prefer to have this check is written explicitly and not implicitly.

drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +0 -0
index 3c11d6e2160a..0e85bebe1dad 100644

drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c static int mlx5_fpga_mem_read_i2c(struct mlx5_fpga_device *fdev, size_t size, · line 74

Quoted 6 lines
drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +3 -0
@@ -69,6 +69,9 @@ static int mlx5_fpga_mem_read_i2c(struct mlx5_fpga_device *fdev, size_t size,
        if (!fdev->mdev)
                return -ENOTCONN;
 
+       if (!size)
+               return -EINVAL;
+

Or simply trust the caller :), and avoid redundant code.

Quoted 41 lines
drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +0 -0
        while (bytes_done < size) {
                actual_size = min(max_size, (size - bytes_done));
 

Thanks

Quoted 36 lines
---
 drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/fpga/sdk.c +2 -3
index 3c11d6e2160a..914fb9d77a1a 100644
@@ -64,7 +64,7 @@ static int mlx5_fpga_mem_read_i2c(struct mlx5_fpga_device *fdev, size_t size,
      size_t max_size = MLX5_FPGA_ACCESS_REG_SIZE_MAX;
      size_t bytes_done = 0;
      u8 actual_size;
-     int err;
+     int err = -EINVAL;
 
      if (!fdev->mdev)
              return -ENOTCONN;
@@ -93,7 +93,7 @@ static int mlx5_fpga_mem_write_i2c(struct mlx5_fpga_device *fdev, size_t size,
      size_t max_size = MLX5_FPGA_ACCESS_REG_SIZE_MAX;
      size_t bytes_done = 0;
      u8 actual_size;
-     int err;
+     int err = -EINVAL;
 
      if (!fdev->mdev)
              return -ENOTCONN;
--

2.9.0

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html