Linting: improve variable scoping 07/260607/2
authorMichal Bloch <m.bloch@samsung.com>
Tue, 29 Jun 2021 18:03:43 +0000 (20:03 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 30 Jun 2021 10:18:03 +0000 (10:18 +0000)
Declare variables at their first site of use where possible, especially
those with the cleanup attribute, but also some arrays that have to be
zeroed before use. This way we won't have wasted time preparing them if
an earlier step fails.

Change-Id: Ic46bdd38915f709bac37c7bfff9ba6dcb19f7657

src/common.c
src/peripheral_adc.c
src/peripheral_gpio.c
src/peripheral_i2c.c
src/peripheral_pwm.c
src/peripheral_spi.c
src/peripheral_uart.c

index bf9a370bfc68cb8052260436c569aab267e56106..969b39ab62279c59aeb7b3e52a0fb4bb838c6e8e 100644 (file)
@@ -46,11 +46,10 @@ bool peripheral_is_feature_supported(const char *feature_name, int *feature_stat
 
 int peripheral_create_udev_monitor(struct udev **udev, struct udev_monitor **monitor, const char *filter_name)
 {
-       struct udev *_udev = NULL;
        struct udev_monitor *_monitor = NULL;
        int ret = -EIO;
 
-       _udev = udev_new();
+       struct udev *_udev = udev_new();
        if (!_udev) {
                _E("Cannot create udev");
                goto error;
@@ -91,7 +90,6 @@ int peripheral_wait_for_udev(struct udev_monitor *monitor, UdevCompareFunc func,
        assert(monitor);
        assert(func);
 
-       struct udev_device *dev = NULL;
        struct pollfd pfd;
 
        pfd.fd = udev_monitor_get_fd(monitor);
@@ -104,7 +102,7 @@ int peripheral_wait_for_udev(struct udev_monitor *monitor, UdevCompareFunc func,
                        return -EIO;
                }
 
-               dev = udev_monitor_receive_device(monitor);
+               struct udev_device *dev = udev_monitor_receive_device(monitor);
                if (dev) {
                        if (func(dev, func_data)) {
                                udev_device_unref(dev);
@@ -121,14 +119,14 @@ int peripheral_write_pin_to_file(const char *file_name, int pin)
 {
        assert(pin >= 0);
 
-       char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */
-
        int fd = open(file_name, O_WRONLY | O_CLOEXEC);
        if (fd < 0) {
                _E("open(%s) failed: %m", file_name);
                return -1;
        }
 
+       char buf[sizeof MAX_d_FMT] = {0, }; /* space for pin %d */
+
        int ret = 0;
        int length = snprintf(buf, sizeof buf, "%d", pin);
        if (write(fd, buf, length) != length)
index 34bd663388b7e9d759b2381d78bdbf79c79eeafe..788d813f934818f02c71ddce876f431acea2abec 100644 (file)
@@ -61,8 +61,7 @@ int peripheral_adc_open(int device, int channel, peripheral_adc_h *adc)
        RETVM_IF(device < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc device number");
        RETVM_IF(channel < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid adc channel number");
 
-       __attribute__ ((cleanup(cleanup_handlep))) peripheral_adc_h handle = NULL;
-       handle = (peripheral_adc_h)calloc(1, sizeof *handle);
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_adc_h handle = (peripheral_adc_h)calloc(1, sizeof *handle);
        if (handle == NULL) {
                _E("Failed to allocate peripheral_adc_h");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
index 84147fcd7113da4da887015bbbdd2debd2d889ea..3d92be59a074fc4524212993238a8c81f4fde573 100644 (file)
@@ -244,11 +244,8 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
        RETVM_IF(gpio == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio handle");
        RETVM_IF(gpio_pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid gpio pin number");
 
-       __attribute__ ((cleanup(cleanup_handlep))) peripheral_gpio_h handle = NULL;
-       __attribute__ ((cleanup(peripheral_unlockp))) int lock = -1;
-
        /* Initialize */
-       handle = (peripheral_gpio_h)calloc(1, sizeof *handle);
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_gpio_h handle = (peripheral_gpio_h)calloc(1, sizeof *handle);
        if (handle == NULL) {
                _E("Failed to allocate peripheral_gpio_h");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
@@ -262,7 +259,7 @@ int peripheral_gpio_open(int gpio_pin, peripheral_gpio_h *gpio)
        handle->fd_value = -1;
        handle->pin = -1;
 
-       lock = peripheral_gpio_lock_export();
+       __attribute__ ((cleanup(peripheral_unlockp))) int lock = peripheral_gpio_lock_export();
        RETVM_IF(lock == -1, PERIPHERAL_ERROR_IO_ERROR, "Failed to lock 'export'");
 
        ret = peripheral_gpio_export(gpio_pin);
@@ -410,8 +407,6 @@ static gpointer __peripheral_gpio_poll(void *data)
 {
        peripheral_gpio_h gpio = (peripheral_gpio_h)data;
 
-       int ret;
-       int poll_state = 0;
        struct pollfd poll_fd;
 
        poll_fd.fd = gpio->fd_value;
@@ -421,7 +416,7 @@ static gpointer __peripheral_gpio_poll(void *data)
 
        while (g_atomic_int_get(&gpio->cb_info.status) == GPIO_INTERRUPTED_CALLBACK_SET) {
 
-               poll_state = poll(&poll_fd, 1, GPIO_INTERRUPTED_CALLBACK_CHECK_SET_INTERVAL_MILISECONDS);
+               int poll_state = poll(&poll_fd, 1, GPIO_INTERRUPTED_CALLBACK_CHECK_SET_INTERVAL_MILISECONDS);
 
                if (poll_state == 0)
                        continue;
@@ -434,7 +429,7 @@ static gpointer __peripheral_gpio_poll(void *data)
                }
 
                if (poll_fd.revents & POLLPRI) {
-                       ret = peripheral_gpio_read(gpio, &value);
+                       int ret = peripheral_gpio_read(gpio, &value);
                        if (ret != PERIPHERAL_ERROR_NONE)
                                continue;
                } else {
index dd3fc42492114205d294e98d24cb84ccfe665c4d..cd64453132b4ae944cd3c14479887447ec6408c1 100644 (file)
@@ -89,9 +89,7 @@ int peripheral_i2c_open_flags(int bus, int address, peripheral_open_flags_e flag
        RETVM_IF(flags != PERIPHERAL_OPEN_FLAGS_PRIVATE && flags != PERIPHERAL_OPEN_FLAGS_SHARED,
                        PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid flags");
 
-       __attribute__ ((cleanup(cleanup_handlep))) peripheral_i2c_h handle = NULL;
-
-       handle = (peripheral_i2c_h)malloc(sizeof *handle);
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_i2c_h handle = (peripheral_i2c_h)malloc(sizeof *handle);
        if (handle == NULL) {
                _E("Failed to allocate peripheral_i2c_h");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
index fd0848aef9646ac65aa00bf9507d6a13fb72b822..80b290744a14ee03f449eb5f654fe93fc929d73e 100644 (file)
@@ -179,23 +179,12 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
        RETVM_IF(pwm == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid pwm handle");
        RETVM_IF(chip < 0 || pin < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
 
-       __attribute__ ((cleanup(peripheral_unlockp))) int lock = -1;
-       __attribute__ ((cleanup(cleanup_handlep))) peripheral_pwm_h handle = NULL;
-       /* space for /sys/class/pwm/pwmchip%d/pwm%d/duty_cycle
-          which is longer than:
-                    /sys/class/pwm/pwmchip%d/pwm%d/period
-                    /sys/class/pwm/pwmchip%d/pwm%d/polarity
-                    /sys/class/pwm/pwmchip%d/pwm%d/enable
-       */
-       char path[PWM_PATH_FMT_MAX_SIZE] = {0, };
-
        /* Initialize */
-       handle = (peripheral_pwm_h)calloc(1, sizeof *handle);
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_pwm_h handle = (peripheral_pwm_h)calloc(1, sizeof *handle);
        if (handle == NULL) {
                _E("Failed to allocate peripheral_pwm_h");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
-
        handle->fd_period = -1;
        handle->fd_duty_cycle = -1;
        handle->fd_polarity = -1;
@@ -203,7 +192,7 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
        handle->chip = -1;
        handle->pin = -1;
 
-       lock = peripheral_pwm_lock_export(chip);
+       __attribute__ ((cleanup(peripheral_unlockp))) int lock = peripheral_pwm_lock_export(chip);
        RETVM_IF(lock == -1, PERIPHERAL_ERROR_IO_ERROR, "Failed to lock 'export'");
 
        ret = peripheral_pwm_export(chip, pin);
@@ -216,6 +205,14 @@ int peripheral_pwm_open(int chip, int pin, peripheral_pwm_h *pwm)
        handle->chip = chip;
        handle->pin = pin;
 
+       /* space for /sys/class/pwm/pwmchip%d/pwm%d/duty_cycle
+          which is longer than:
+                    /sys/class/pwm/pwmchip%d/pwm%d/period
+                    /sys/class/pwm/pwmchip%d/pwm%d/polarity
+                    /sys/class/pwm/pwmchip%d/pwm%d/enable
+       */
+       char path[PWM_PATH_FMT_MAX_SIZE] = {0, };
+
        snprintf(path, sizeof path, PWM_PATH_FMT("period"), chip, pin);
        handle->fd_period = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd_period < 0);
index 059c2b06a3044a2aa31b2b618ac2f8eae8f3f300..8e84a29b7f6db1810b4e12a7cb8e64af1ce27608 100644 (file)
@@ -52,21 +52,20 @@ static inline void cleanup_handlep(peripheral_spi_h *handle)
 
 int peripheral_spi_open(int bus, int cs, peripheral_spi_h *spi)
 {
-       char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/spidev%d.%d */
 
        RETVM_IF(!__is_feature_supported(), PERIPHERAL_ERROR_NOT_SUPPORTED, "SPI feature is not supported");
        RETVM_IF(spi == NULL, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid spi handle");
        RETVM_IF(bus < 0 || cs < 0, PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid parameter");
 
-       __attribute__ ((cleanup(cleanup_handlep))) peripheral_spi_h handle = NULL;
-
        /* Initialize */
-       handle = (peripheral_spi_h)calloc(1, sizeof *handle);
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_spi_h handle = (peripheral_spi_h)calloc(1, sizeof *handle);
        if (handle == NULL) {
                _E("Failed to allocate peripheral_spi_h");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;
        }
 
+       char path[DEV_PATH_FMT_MAX_SIZE] = {0, }; /* space for /dev/spidev%d.%d */
+
        snprintf(path, sizeof path, DEV_PATH_FMT, bus, cs);
        handle->fd = open(path, O_RDWR | O_CLOEXEC);
        CHECK_ERROR(handle->fd < 0);
index f4f73571f4e1d239d777ea5e95559115af9d672a..6a649df7bec62d0da1486670f4135223cd2d320b 100644 (file)
@@ -76,8 +76,7 @@ int peripheral_uart_open_flags(int port, peripheral_open_flags_e flags, peripher
        RETVM_IF(flags != PERIPHERAL_OPEN_FLAGS_PRIVATE && flags != PERIPHERAL_OPEN_FLAGS_SHARED,
                        PERIPHERAL_ERROR_INVALID_PARAMETER, "Invalid flags");
 
-       __attribute__ ((cleanup(cleanup_handlep))) peripheral_uart_h handle = NULL;
-       handle = (peripheral_uart_h)calloc(1, sizeof *handle);
+       __attribute__ ((cleanup(cleanup_handlep))) peripheral_uart_h handle = (peripheral_uart_h)calloc(1, sizeof *handle);
        if (handle == NULL) {
                _E("Failed to allocate peripheral_uart_h");
                return PERIPHERAL_ERROR_OUT_OF_MEMORY;