Fix file descriptor leak on error
Detected by cppcheck Signed-off-by: Thomas Jarosch <thomas.jarosch@intra2net.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
parent
de213328f8
commit
6971932254
16
lib/utils.c
16
lib/utils.c
|
@ -84,24 +84,32 @@ int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
num = strtol(buf, &end, 0);
|
num = strtol(buf, &end, 0);
|
||||||
if (end == buf)
|
if (end == buf) {
|
||||||
|
fclose(fd);
|
||||||
return -NLE_INVAL;
|
return -NLE_INVAL;
|
||||||
|
}
|
||||||
|
|
||||||
if (num == LONG_MIN || num == LONG_MAX)
|
if (num == LONG_MIN || num == LONG_MAX) {
|
||||||
|
fclose(fd);
|
||||||
return -NLE_RANGE;
|
return -NLE_RANGE;
|
||||||
|
}
|
||||||
|
|
||||||
while (*end == ' ' || *end == '\t')
|
while (*end == ' ' || *end == '\t')
|
||||||
end++;
|
end++;
|
||||||
|
|
||||||
goodlen = strcspn(end, "#\r\n\t ");
|
goodlen = strcspn(end, "#\r\n\t ");
|
||||||
if (goodlen == 0)
|
if (goodlen == 0) {
|
||||||
|
fclose(fd);
|
||||||
return -NLE_INVAL;
|
return -NLE_INVAL;
|
||||||
|
}
|
||||||
|
|
||||||
end[goodlen] = '\0';
|
end[goodlen] = '\0';
|
||||||
|
|
||||||
err = cb(num, end);
|
err = cb(num, end);
|
||||||
if (err < 0)
|
if (err < 0) {
|
||||||
|
fclose(fd);
|
||||||
return err;
|
return err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
Reference in New Issue