battery: Modify the length of strncmp parameter 68/314568/1 accepted/tizen_unified accepted/tizen_unified_dev accepted/tizen_unified_x tizen accepted/tizen/unified/20240716.112416 accepted/tizen/unified/20240716.140259 accepted/tizen/unified/dev/20240717.110309 accepted/tizen/unified/x/20240717.012438
authorYunhee Seo <yuni.seo@samsung.com>
Mon, 15 Jul 2024 11:34:58 +0000 (20:34 +0900)
committerYunhee Seo <yuni.seo@samsung.com>
Mon, 15 Jul 2024 11:34:58 +0000 (20:34 +0900)
When comparing two strings, it is not clear what values are contained in the case where they are obtained from an array.
Thus, using sizeof literal is more clear in terms of readability and maintenance.
This is meaningful especially when the array does not contain proper values or is empty.

Change-Id: Id1f24665910e4f754e7c77d89ecd036839309add
Signed-off-by: Yunhee Seo <yuni.seo@samsung.com>
src/battery.c

index 96303a5..c79e203 100644 (file)
@@ -188,7 +188,6 @@ int device_battery_get_health(device_battery_health_e *health)
 {
        struct device_battery_info info;
        int ret;
-       size_t len;
 
        if (!health)
                return DEVICE_ERROR_INVALID_PARAMETER;
@@ -203,16 +202,15 @@ int device_battery_get_health(device_battery_health_e *health)
                return ret;
        }
 
-       len = strlen(info.health);
-       if (!strncmp(info.health, "Good", len))
+       if (!strncmp(info.health, "Good", sizeof("Good")))
                *health = DEVICE_BATTERY_HEALTH_GOOD;
-       else if (!strncmp(info.health, "Cold", len))
+       else if (!strncmp(info.health, "Cold", sizeof("Cold")))
                *health = DEVICE_BATTERY_HEALTH_COLD;
-       else if (!strncmp(info.health, "Dead", len))
+       else if (!strncmp(info.health, "Dead", sizeof("Dead")))
                *health = DEVICE_BATTERY_HEALTH_DEAD;
-       else if (!strncmp(info.health, "Overheat", len))
+       else if (!strncmp(info.health, "Overheat", sizeof("Overheat")))
                *health = DEVICE_BATTERY_HEALTH_OVER_HEAT;
-       else if (!strncmp(info.health, "Over voltage", len))
+       else if (!strncmp(info.health, "Over voltage", sizeof("Over voltage")))
                *health = DEVICE_BATTERY_HEALTH_OVER_VOLTAGE;
        else
                return DEVICE_ERROR_OPERATION_FAILED;
@@ -226,7 +224,6 @@ int device_battery_get_power_source(device_battery_power_source_e *source)
 {
        struct device_battery_info info;
        int ret;
-       size_t len;
 
        if (!source)
                return DEVICE_ERROR_INVALID_PARAMETER;
@@ -241,12 +238,11 @@ int device_battery_get_power_source(device_battery_power_source_e *source)
                return ret;
        }
 
-       len = strlen(info.power_source);
-       if (!strncmp(info.power_source, "ac", len))
+       if (!strncmp(info.power_source, "ac", sizeof("ac")))
                *source = DEVICE_BATTERY_POWER_SOURCE_AC;
-       else if (!strncmp(info.power_source, "usb", len))
+       else if (!strncmp(info.power_source, "usb", sizeof("usb")))
                *source = DEVICE_BATTERY_POWER_SOURCE_USB;
-       else if (!strncmp(info.power_source, "wireless", len))
+       else if (!strncmp(info.power_source, "wireless", sizeof("wireless")))
                *source = DEVICE_BATTERY_POWER_SOURCE_WIRELESS;
        else
                *source = DEVICE_BATTERY_POWER_SOURCE_NONE;
@@ -307,7 +303,6 @@ int device_battery_get_status(device_battery_status_e *status)
 {
        struct device_battery_info info;
        int ret;
-       size_t len;
 
        if (!status)
                return DEVICE_ERROR_INVALID_PARAMETER;
@@ -322,14 +317,13 @@ int device_battery_get_status(device_battery_status_e *status)
                return ret;
        }
 
-       len = strlen(info.status);
-       if (!strncmp(info.status, "Charging", len))
+       if (!strncmp(info.status, "Charging", sizeof("Charging")))
                *status = DEVICE_BATTERY_STATUS_CHARGING;
-       else if (!strncmp(info.status, "Discharging", len))
+       else if (!strncmp(info.status, "Discharging", sizeof("Discharging")))
                *status = DEVICE_BATTERY_STATUS_DISCHARGING;
-       else if (!strncmp(info.status, "Full", len))
+       else if (!strncmp(info.status, "Full", sizeof("Full")))
                *status = DEVICE_BATTERY_STATUS_FULL;
-       else if (!strncmp(info.status, "Not charging", len))
+       else if (!strncmp(info.status, "Not charging", sizeof("Not charging")))
                *status = DEVICE_BATTERY_STATUS_NOT_CHARGING;
        else
                return DEVICE_ERROR_OPERATION_FAILED;