Commit 981089cf authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Imported scnprintf/vscnprintf from Linux kernel - turns out, we were using...

Imported scnprintf/vscnprintf from Linux kernel - turns out, we were using snprintf wrong - https://lwn.net/Articles/69419/
(disallow using the return value of snprintf/vsnprintf, as all our usage assumed non-standard behavior)

--HG--
branch : vsnprintf
parent 9f9fd183c6de
......@@ -764,14 +764,14 @@ void eos_backtrace_rebuild(EOSState *s, char * buf, int size)
if (buf)
{
int len = snprintf(buf, size,
int len = scnprintf(buf, size,
"%s stack: %x [%x-%x]\n",
eos_get_current_task_name(s),
sps[0], stack_top, stack_bot
);
while (--i >= 0)
{
len += snprintf(
len += scnprintf(
buf + len, size - len,
"%s @ %x:%x\n",
called_func(lrs[i] - 4), lrs[i] - 4, sps[i]
......
......@@ -590,7 +590,7 @@ const char* get_assert_msg() { return assert_msg; }
static int my_assert_handler(char* msg, char* file, int line, int arg4)
{
int len = snprintf(assert_msg, sizeof(assert_msg),
int len = scnprintf(assert_msg, sizeof(assert_msg),
"ASSERT: %s\n"
"at %s:%d, task %s\n"
"lv:%d mode:%d\n\n",
......@@ -605,7 +605,7 @@ static int my_assert_handler(char* msg, char* file, int line, int arg4)
void ml_assert_handler(char* msg, char* file, int line, const char* func)
{
int len = snprintf(assert_msg, sizeof(assert_msg),
int len = scnprintf(assert_msg, sizeof(assert_msg),
"ML ASSERT:\n%s\n"
"at %s:%d (%s), task %s\n"
"lv:%d mode:%d\n\n",
......
......@@ -324,7 +324,7 @@ int printf(const char* fmt, ...)
va_list ap;
va_start( ap, fmt );
int len = vsnprintf( buf, buf_size-1, fmt, ap );
int len = vscnprintf( buf, buf_size, fmt, ap );
va_end( ap );
console_puts(buf);
return len;
......
......@@ -173,7 +173,7 @@ static void dump_img_task(void* priv, int unused)
char* display_device = get_display_device_name();
int path_len = snprintf(pattern, sizeof(pattern), "%s/%s/%s/", CAMERA_MODEL, video_mode, display_device);
int path_len = scnprintf(pattern, sizeof(pattern), "%s/%s/%s/", CAMERA_MODEL, video_mode, display_device);
/* make sure the VRAM parameters are updated */
get_yuv422_vram();
......
......@@ -162,12 +162,23 @@ extern void msleep( int amount );
extern int call( const char* name, ... );
// stdio
extern int vsnprintf( char* str, size_t n, const char* fmt, va_list ap ); // non-standard; don't export it
extern int printf(const char* fmt, ... );
extern int puts(const char* s);
extern size_t strlen( const char* str );
extern int snprintf( char* str, size_t n, const char* fmt, ... );
extern int scnprintf( char* str, size_t n, const char* fmt, ... );
extern int vscnprintf( char* str, size_t n, const char* fmt, va_list ap ); // non-standard; don't export it
/* since Canon's vsnprintf returns the number of chars actually written,
* we were using the return value of snprintf/vsnprintf
* as described in https://lwn.net/Articles/69419/
* disallow this usage; we really meant scnprintf/vscnprintf
* note: if we don't care about return value, they are the same as snprintf/vsnprintf
*/
#define snprintf (void)scnprintf
#define vsnprintf (void)vscnprintf
extern int strcmp( const char* s1, const char* s2 );
extern int strncmp( const char* s1, const char* s2, size_t n);
extern int strcasecmp( const char* s1, const char* s2 );
......
......@@ -5472,7 +5472,7 @@ static void menu_set_flags(char* menu_name, char* entry_name, int flags)
jhide_menu_by_name(menu_name, entry_name);
}
#define CFG_APPEND(fmt, ...) ({ lastlen = snprintf(cfg + cfglen, CFG_SIZE - cfglen, fmt, ## __VA_ARGS__); cfglen += lastlen; })
#define CFG_APPEND(fmt, ...) ({ int lastlen = scnprintf(cfg + cfglen, CFG_SIZE - cfglen, fmt, ## __VA_ARGS__); cfglen += lastlen; })
#define CFG_SIZE 32768
static void menu_save_flags(char* filename)
......@@ -5480,7 +5480,6 @@ static void menu_save_flags(char* filename)
char* cfg = malloc(CFG_SIZE);
cfg[0] = '\0';
int cfglen = 0;
int lastlen = 0;
struct menu * menu = menus;
for( ; menu ; menu = menu->next )
......
......@@ -11,6 +11,64 @@
#include "dryos.h"
//#include <errno.h>
/* from https://github.com/torvalds/linux/blob/master/vsprintf.c
* Canon's vsnprintf returns the number of characters actually written,
* so we were using snprintf like this, even on desktop tools...
* see https://lwn.net/Articles/69419/ */
#undef vsnprintf
/**
* vscnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space
* @fmt: The format string to use
* @args: Arguments for the format string
*
* The return value is the number of characters which have been written into
* the @buf not including the trailing '\0'. If @size is == 0 the function
* returns 0.
*
* If you're not already dealing with a va_list consider using scnprintf().
*
* See the vsnprintf() documentation for format string extensions over C99.
*/
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
unsigned int i;
i = vsnprintf(buf, size, fmt, args);
if (likely(i < size))
return i;
if (size != 0)
return size - 1;
return 0;
}
/**
* scnprintf - Format a string and place it in a buffer
* @buf: The buffer to place the result into
* @size: The size of the buffer, including the trailing null space
* @fmt: The format string to use
* @...: Arguments for the format string
*
* The return value is the number of characters written into @buf not including
* the trailing '\0'. If @size is == 0 the function returns 0.
*/
int scnprintf(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i = vscnprintf(buf, size, fmt, args);
va_end(args);
return i;
}
// sometimes gcc likes very much the default fprintf and uses that one
// => renamed to my_fprintf to force it to use this one
int
......@@ -27,7 +85,7 @@ my_fprintf(
char buf[maxlen];
va_start( ap, fmt );
len = vsnprintf( buf, maxlen-1, fmt, ap );
len = vscnprintf( buf, maxlen, fmt, ap );
va_end( ap );
FIO_WriteFile( file, buf, len );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment