From 90b0e28456560f3509cc03c5648d77d5b3f9ef36 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 26 Nov 2014 09:53:52 +0100 Subject: i2c: Do not close file descriptor 0 if open fails --- cpukit/dev/i2c/i2c-dev.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'cpukit/dev') diff --git a/cpukit/dev/i2c/i2c-dev.c b/cpukit/dev/i2c/i2c-dev.c index cf1cf41ba8..76ae757eb0 100644 --- a/cpukit/dev/i2c/i2c-dev.c +++ b/cpukit/dev/i2c/i2c-dev.c @@ -218,19 +218,19 @@ static int i2c_dev_do_init( void (*destroy)(i2c_dev *dev) ) { - int fd; int rv; - fd = open(bus_path, O_RDWR); - if (fd < 0) { + dev->bus_fd = open(bus_path, O_RDWR); + if (dev->bus_fd < 0) { (*destroy)(dev); + return -1; } - rv = ioctl(fd, I2C_BUS_GET_CONTROL, &dev->bus); + rv = ioctl(dev->bus_fd, I2C_BUS_GET_CONTROL, &dev->bus); if (rv != 0) { - (void) close(fd); (*destroy)(dev); + return -1; } @@ -240,7 +240,6 @@ static int i2c_dev_do_init( dev->get_size = i2c_dev_get_size_default; dev->get_block_size = i2c_dev_get_block_size_default; dev->destroy = destroy; - dev->bus_fd = fd; dev->address = address; return 0; @@ -251,7 +250,7 @@ void i2c_dev_destroy(i2c_dev *dev) int rv; rv = close(dev->bus_fd); - _Assert(rv == 0); + _Assert(dev->bus_fd < 0 || rv == 0); (void) rv; } -- cgit v1.2.3