{
const char *value;
int fd, newfd, flags, ret;
+ char *end;
+ errno = 0;
value = (const char*)getenv(env);
if (!value)
return -1;
- ret = sscanf(value, "%d", &fd);
- if (ret < 0) {
- TDM_ERR("sscanf failed: %m");
+ const long sl = strtol(value, &end, 10);
+ if (end == value) {
+ TDM_ERR("%s: not a decimal number\n", value);
return -1;
+ } else if (*end != '\0') {
+ TDM_ERR("%s: extra characters at end of input: %s\n", value, end);
+ return -1;
+ } else if ((sl == LONG_MIN || sl == LONG_MAX) && errno == ERANGE) {
+ TDM_ERR("%s out of range of type long\n", value);
+ return -1;
+ } else if (sl > INT_MAX) {
+ TDM_ERR("%ld greater than INT_MAX\n", sl);
+ return -1;
+ } else if (sl < INT_MIN) {
+ TDM_ERR("%ld less than INT_MIN\n", sl);
+ return -1;
+ } else {
+ fd = (int)sl;
+ if (fd < 0) {
+ TDM_ERR("%d out of fd range\n", fd);
+ return -1;
+ }
}
flags = fcntl(fd, F_GETFD);