commit 09d7031cb462889392090e71991a89c522d387bc
Author: Lee Duncan <lduncan@suse.com>
Date: Mon Sep 24 16:37:19 2018 -0700
Fix reading of sysfs signed integers when negative.
The code for reading all sysfs integer types (of all
sizes) did not work when reading signed integers and
the return value was negative. So when the default was -1
and the value was not present, the code tried to return -1.
But the logic for checking against "max value" was
incorrect, causing INT_MAX to be returned instead of -1.
---
libopeniscsiusr/sysfs.c | 32 +++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)
diff --git a/libopeniscsiusr/sysfs.c b/libopeniscsiusr/sysfs.c
index 08f71b317c55..c4f89a31aca0 100644
--- a/libopeniscsiusr/sysfs.c
+++ b/libopeniscsiusr/sysfs.c
@@ -47,7 +47,7 @@
#define _SYS_NULL_STR "(null)"
-#define _sysfs_prop_get_int_func_gen(func_name, out_type, type_max_value) \
+#define _sysfs_prop_get_uint_func_gen(func_name, out_type, type_max_value) \
int func_name(struct iscsi_context *ctx, const char *dir_path, \
const char *prop_name, out_type *val, \
out_type default_value, bool ignore_error) \
@@ -63,6 +63,28 @@
return rc; \
}
+#define _sysfs_prop_get_int_func_gen(func_name, out_type, type_min_value, type_max_value) \
+ int func_name(struct iscsi_context *ctx, const char *dir_path, \
+ const char *prop_name, out_type *val, \
+ out_type default_value, bool ignore_error) \
+ { \
+ long long int tmp_val = 0; \
+ int rc = LIBISCSI_OK; \
+ long long int dv = default_value; \
+ rc = iscsi_sysfs_prop_get_ll(ctx, dir_path, prop_name, \
+ &tmp_val, (long long int) dv, \
+ ignore_error); \
+ if (rc == LIBISCSI_OK) { \
+ if (tmp_val > type_max_value) \
+ *val = type_max_value; \
+ else if (tmp_val < type_min_value) \
+ *val = type_min_value; \
+ else \
+ *val = tmp_val; \
+ } \
+ return rc; \
+ }
+
enum _sysfs_dev_class {
_SYSFS_DEV_CLASS_ISCSI_SESSION,
@@ -82,10 +104,10 @@ static int iscsi_sysfs_prop_get_ll(struct iscsi_context *ctx,
static int sysfs_get_dev_path(struct iscsi_context *ctx, const char *path,
enum _sysfs_dev_class class, char **dev_path);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_u8, uint8_t, UINT8_MAX);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_u16, uint16_t, UINT16_MAX);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_i32, int32_t, INT32_MAX);
-_sysfs_prop_get_int_func_gen(_sysfs_prop_get_u32, uint32_t, UINT32_MAX);
+_sysfs_prop_get_uint_func_gen(_sysfs_prop_get_u8, uint8_t, UINT8_MAX);
+_sysfs_prop_get_uint_func_gen(_sysfs_prop_get_u16, uint16_t, UINT16_MAX);
+_sysfs_prop_get_int_func_gen(_sysfs_prop_get_i32, int32_t, INT32_MIN, INT32_MAX);
+_sysfs_prop_get_uint_func_gen(_sysfs_prop_get_u32, uint32_t, UINT32_MAX);
static int sysfs_read_file(const char *path, uint8_t *buff, size_t buff_size)
{