mirror_zfs/module/lua/lcompat.c
Don Brady cbce581353 Fix coverity defects: zfs channel programs
CID 173243, 173245:  Memory - corruptions  (OVERRUN)
 Added size argument to lcompat_sprintf() to avoid use of INT_MAX

CID 173244:  Integer handling issues  (OVERFLOW_BEFORE_WIDEN)
 Added cast to uint64_t to avoid a 32 bit overflow warning

CID 173242:  Integer handling issues  (CONSTANT_EXPRESSION_RESULT)
 Conditionally removed unused luai_numisnan() floating point check

CID 173241:  Resource leaks  (RESOURCE_LEAK)
 Added missing close(fd) on error path

CID 173240:    (UNINIT)
Fixed uninitialized variable in get_special_prop()

CID 147560:  Null pointer dereferences  (NULL_RETURNS)
Cleaned up bad code merge in dsl_dataset_promote_check()

CID 28475:  Memory - illegal accesses  (OVERRUN)
Fixed lcompat_sprintf() to use a size paramater

CID 28418, 28422:  Error handling issues  (CHECKED_RETURN)
Added function result cast to (void) to avoid warning

CID 23935, 28411, 28412:  Memory - corruptions  (ARRAY_VS_SINGLETON)
Added casts to avoid exposing result as an array

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Don Brady <don.brady@delphix.com>
Closes #7181
2018-02-20 11:19:42 -08:00

102 lines
1.4 KiB
C

/*
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
#include <sys/lua/lua.h>
ssize_t
lcompat_sprintf(char *buf, size_t size, const char *fmt, ...)
{
ssize_t res;
va_list args;
va_start(args, fmt);
res = vsnprintf(buf, size, fmt, args);
va_end(args);
return (res);
}
int64_t
lcompat_strtoll(const char *str, char **ptr)
{
int base;
const char *cp;
int digits;
int64_t value;
boolean_t is_negative;
cp = str;
while (*cp == ' ' || *cp == '\t' || *cp == '\n') {
cp++;
}
is_negative = (*cp == '-');
if (is_negative) {
cp++;
}
base = 10;
if (*cp == '0') {
base = 8;
cp++;
if (*cp == 'x' || *cp == 'X') {
base = 16;
cp++;
}
}
value = 0;
for (; *cp != '\0'; cp++) {
if (*cp >= '0' && *cp <= '9') {
digits = *cp - '0';
} else if (*cp >= 'a' && *cp <= 'f') {
digits = *cp - 'a' + 10;
} else if (*cp >= 'A' && *cp <= 'F') {
digits = *cp - 'A' + 10;
} else {
break;
}
if (digits >= base) {
break;
}
value = (value * base) + digits;
}
if (ptr != NULL) {
*ptr = (char *)cp;
}
if (is_negative) {
value = -value;
}
return (value);
}
int64_t
lcompat_pow(int64_t x, int64_t y)
{
int64_t result = 1;
if (y < 0)
return (0);
while (y) {
if (y & 1)
result *= x;
y >>= 1;
x *= x;
}
return (result);
}
int
lcompat_hashnum(int64_t x)
{
x = (~x) + (x << 18);
x = x ^ (x >> 31);
x = x * 21;
x = x ^ (x >> 11);
x = x + (x << 6);
x = x ^ (x >> 22);
return ((int)x);
}