[common] numerious bad usage bug fixes

This commit is contained in:
Geoffrey McRae 2020-01-10 15:23:49 +11:00
parent 76fa390e3d
commit 22f04a926f
8 changed files with 32 additions and 14 deletions

View File

@ -1 +1 @@
B1-66-g0aa8711796+1 B1-67-g1ef406bbaf+1

View File

@ -32,4 +32,5 @@ struct IVSHMEM
void ivshmemOptionsInit(); void ivshmemOptionsInit();
bool ivshmemOpen(struct IVSHMEM * dev); bool ivshmemOpen(struct IVSHMEM * dev);
bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDev);
void ivshmemClose(struct IVSHMEM * dev); void ivshmemClose(struct IVSHMEM * dev);

View File

@ -18,4 +18,5 @@ Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
// sprintf but with buffer allocation // sprintf but with buffer allocation
int alloc_sprintf(char ** str, const char * format, ...); int alloc_sprintf(char ** str, const char * format, ...)
__attribute__ ((format (printf, 2, 3)));

View File

@ -600,7 +600,7 @@ void option_print()
maxLen = alloc_sprintf( maxLen = alloc_sprintf(
&line, &line,
"%-*s | Short | %-*s | Description", "%-*s | Short | %-*s | Description",
strlen(state.groups[g].module) + state.groups[g].pad + 1, (int)(strlen(state.groups[g].module) + state.groups[g].pad + 1),
"Long", "Long",
valueLen, valueLen,
"Value" "Value"

View File

@ -17,4 +17,7 @@ if(ENABLE_BACKTRACE)
target_link_libraries(lg_common_platform_code bfd) target_link_libraries(lg_common_platform_code bfd)
endif() endif()
target_link_libraries(lg_common_platform_code pthread) target_link_libraries(lg_common_platform_code
lg_common
pthread
)

View File

@ -42,7 +42,6 @@ static int uioOpenFile(const char * shmDevice, const char * file)
{ {
char * path; char * path;
alloc_sprintf(&path, "/sys/class/uio/%s/%s", shmDevice, file); alloc_sprintf(&path, "/sys/class/uio/%s/%s", shmDevice, file);
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd < 0) if (fd < 0)
{ {
@ -161,10 +160,14 @@ void ivshmemOptionsInit()
} }
bool ivshmemOpen(struct IVSHMEM * dev) bool ivshmemOpen(struct IVSHMEM * dev)
{
return ivshmemOpenDev(dev, option_get_string("app", "shmFile"));
}
bool ivshmemOpenDev(struct IVSHMEM * dev, const char * shmDevice)
{ {
assert(dev); assert(dev);
const char * shmDevice = option_get_string("app", "shmFile");
unsigned int devSize; unsigned int devSize;
int devFD; int devFD;
@ -257,5 +260,7 @@ void ivshmemClose(struct IVSHMEM * dev)
close(info->fd); close(info->fd);
free(info); free(info);
dev->mem = NULL;
dev->size = 0;
dev->opaque = NULL; dev->opaque = NULL;
} }

View File

@ -15,5 +15,6 @@ add_library(lg_common_platform_code STATIC
) )
target_link_libraries(lg_common_platform_code target_link_libraries(lg_common_platform_code
lg_common
setupapi setupapi
) )

View File

@ -21,26 +21,24 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
int alloc_sprintf(char ** str, const char * format, ...) static int valloc_sprintf(char ** str, const char * format, va_list ap)
{ {
if (!str) if (!str)
return -1; return -1;
*str = NULL; *str = NULL;
va_list ap;
va_start(ap, format); va_list ap1;
int len = vsnprintf(NULL, 0, format, ap); va_copy(ap1, ap);
va_end(ap); int len = vsnprintf(NULL, 0, format, ap1);
va_end(ap1);
if (len < 0) if (len < 0)
return len; return len;
*str = malloc(len+1); *str = malloc(len+1);
va_start(ap, format);
int ret = vsnprintf(*str, len + 1, format, ap); int ret = vsnprintf(*str, len + 1, format, ap);
va_end(ap);
if (ret < 0) if (ret < 0)
{ {
free(*str); free(*str);
@ -48,5 +46,14 @@ int alloc_sprintf(char ** str, const char * format, ...)
return ret; return ret;
} }
return ret;
}
int alloc_sprintf(char ** str, const char * format, ...)
{
va_list ap;
va_start(ap, format);
int ret = valloc_sprintf(str, format, ap);
va_end(ap);
return ret; return ret;
} }