Commit 65935c11 authored by Peter Pentchev's avatar Peter Pentchev

Use snprintf() instead of sprintf(), strcpy(), and strcat().

parent eb9ff560
......@@ -10,6 +10,7 @@ Changelog for libdebug.
- tests/Makefile: rebuild the executables if the library changes
- hex: fix hexdump_stub() for 16*n-byte blocks!
- test_hex: accept "-" as an alias for the standard input stream
- use snprintf() instead of sprintf(), strcpy(), and strcat()
0.4.4 2011/07/19
- fix a compiler warning about an unused variable in memory.c
......
......@@ -41,43 +41,35 @@ static __inline__ int printable (int c)
return c >= 32 && c <= 126;
}
#define BUF_SIZE 81
static void dump16 (char *buf,const uint8_t *s,uintptr_t offset,size_t len)
{
size_t i,n;
n = sprintf (buf,"%08lx - ",(unsigned long)offset);
n = snprintf (buf, BUF_SIZE, "%08lx - ",(unsigned long)offset);
for (i = 0; i < len; i++)
{
if (i && !(i & 3))
buf[n++] = ' ';
n += sprintf (buf + n,"%02x ",s[i]);
}
n += snprintf(buf + n, BUF_SIZE - n, "%s%02x ",
i && !(i & 3)? " ": "", s[i]);
for ( ; i < 16; i++)
{
if (i && !(i & 3))
buf[n++] = ' ';
buf[n++] = ' ';
buf[n++] = ' ';
buf[n++] = ' ';
}
n += snprintf(buf + n, BUF_SIZE - n, "%s ",
i && !(i & 3)? " ": "");
buf[n++] = ' ';
n += snprintf(buf + n, BUF_SIZE - n, " ");
for (i = 0; i < len; i++)
buf[n++] = printable (s[i]) ? s[i] : '.';
n += snprintf(buf + n, BUF_SIZE - n, "%c",
printable (s[i]) ? s[i] : '.');
buf[n++] = '\n';
buf[n] = '\0';
n += snprintf(buf + n, BUF_SIZE - n, "\n");
}
void hexdump_stub (const char *filename,int line,const char *function,int level,
const void *ptr,size_t size)
{
char buf[81];
char buf[BUF_SIZE];
const uint8_t *s = ptr;
size_t i;
......
......@@ -251,9 +251,10 @@ static char *prefix (struct log_private *priv,const char *filename,int line,cons
*buf[4] = '\0';
size_t wr = 0;
for (i = 0; i < n; i++)
{
strcat (buf[4],buf[i]);
wr += snprintf(buf[4] + wr, len + 1 - wr, "%s", buf[i]);
mem_free (buf[i]);
}
......@@ -273,17 +274,18 @@ static int line_append (struct log_private *priv,const char *str)
if (priv->str != NULL)
{
if ((ptr = mem_realloc (priv->str,(len + strlen (priv->str)) * sizeof (char))) == NULL)
size_t priv_len = strlen(priv->str);
if ((ptr = mem_realloc (priv->str,(len + priv_len) * sizeof (char))) == NULL)
return (-1);
priv->str = ptr;
strcat (priv->str,str);
snprintf(priv->str + priv_len, len + priv_len, "%s", str);
}
else
{
if ((priv->str = mem_alloc (len * sizeof (char))) == NULL)
return (-1);
strcpy (priv->str,str);
snprintf(priv->str, len, "%s", str);
}
return (0);
......@@ -363,9 +365,10 @@ static int flush_cache (struct log_private *priv,const char *filename,int line,c
mem_free (buf);
sprintf (priv->cache.buf,"Message repeated %d times\n",priv->cache.hits + 1);
char b[35];
snprintf (b, sizeof(b), "Message repeated %d times\n", priv->cache.hits + 1);
if (savetodisk (priv->cache.fd,priv->cache.buf) < 0)
if (savetodisk (priv->cache.fd, b) < 0)
goto error;
}
......@@ -402,7 +405,7 @@ static int line_end (struct log_private *priv,const char *filename,int line,cons
{
if (priv->cache.cache != NULL)
{
if (strcmp (priv->str,priv->cache.cache))
if (strcmp (priv->str,priv->cache.cache) != 0)
{
if (flush_cache (priv,filename,line,function) < 0)
goto error;
......@@ -537,23 +540,28 @@ static int log_open_syslog (const char *str)
char *facility;
size_t i;
if ((log_private.ident = mem_alloc (strlen (str) + 1)) == NULL)
char *fac_separator = strchr(str, '.');
if (fac_separator == NULL)
return (-1);
strcpy (log_private.ident,str);
if ((facility = strchr (log_private.ident,'.')) == NULL)
size_t sz = strlen(str) + 1;
if ((log_private.ident = mem_alloc (sz)) == NULL)
return (-1);
snprintf(log_private.ident, sz, "%s", str);
facility = log_private.ident + (fac_separator - str);
*facility++ = '\0';
for (i = 0; facilitynames[i].c_name != NULL; i++)
if (!strcmp (facility,facilitynames[i].c_name))
if (strcmp (facility,facilitynames[i].c_name) == 0)
{
openlog (log_private.ident,LOG_PID,facilitynames[i].c_val);
return (0);
}
mem_free(log_private.ident);
log_private.ident = NULL;
return (-1);
}
......@@ -568,7 +576,7 @@ int log_open (const char *logfile,int loglevel,int flags)
if (logfile != NULL)
{
flags &= ~(LOG_HAVE_COLORS | LOG_HAVE_LOGFILE | LOG_USE_SYSLOG);
flags |= !log_open_syslog (logfile) ? LOG_USE_SYSLOG : LOG_HAVE_LOGFILE;
flags |= log_open_syslog (logfile) == 0 ? LOG_USE_SYSLOG : LOG_HAVE_LOGFILE;
if (flags & LOG_USE_SYSLOG)
flags &= ~LOG_DETECT_DUPLICATES;
......@@ -579,7 +587,8 @@ int log_open (const char *logfile,int loglevel,int flags)
{
int saved;
if ((log_private.filename = mem_alloc ((strlen (logfile) + 1) * sizeof (char))) == NULL)
size_t sz = strlen(logfile) + 1;
if ((log_private.filename = mem_alloc (sz * sizeof (char))) == NULL)
return (-1);
if ((log_private.fd = open (logfile,O_CREAT | O_APPEND | O_WRONLY,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
......@@ -590,7 +599,7 @@ int log_open (const char *logfile,int loglevel,int flags)
return (-1);
}
strcpy (log_private.filename,logfile);
snprintf(log_private.filename, sz, "%s", logfile);
}
log_private.level = loglevel;
......@@ -725,10 +734,11 @@ int log_puts_stub (const char *filename,int line,const char *function,int level,
if (level == _LOG_QUIET || log_private.level == _LOG_QUIET || level > log_private.level)
return (0);
if ((s = mem_alloc (sizeof (char) * (strlen (str) + 1))) == NULL)
size_t sz = strlen(str) + 1;
if ((s = mem_alloc (sizeof (char) * sz)) == NULL)
return (-1);
strcpy (s,str);
snprintf(s, sz, "%s", str);
result = write_string (&log_private,filename,line,function,level,s);
mem_free (s);
......
......@@ -137,13 +137,13 @@ void mem_check_stub (const char *file,int line,const char *function)
{
if (*(tmp->head_sig) != MEM_SIGNATURE)
{
sprintf (str,"Overwrite error: Bad head signature in area %p\n",tmp->data);
snprintf (str, sizeof(str), "Overwrite error: Bad head signature in area %p\n", tmp->data);
mem_abort1 (str,tmp,file,line,function);
}
if (*(tmp->tail_sig) != MEM_SIGNATURE)
{
sprintf (str,"Overwrite error: Bad tail signature in area %p\n",tmp->data);
snprintf (str, sizeof(str), "Overwrite error: Bad tail signature in area %p\n", tmp->data);
mem_abort1 (str,tmp,file,line,function);
}
}
......@@ -204,7 +204,7 @@ void *mem_realloc_stub (void *ptr,size_t size,const char *file,int line,const ch
if (tmp == NULL)
{
sprintf (str,"Attempted to realloc unallocated pointer: %p\n",ptr);
snprintf (str, sizeof(str), "Attempted to realloc unallocated pointer: %p\n", ptr);
mem_abort2 (str,file,line,function);
}
......@@ -255,7 +255,7 @@ void mem_free_stub (void *ptr,const char *file,int line,const char *function)
if (cur == NULL)
{
sprintf (str,"Attempted to free an unallocated pointer: %p\n",ptr);
snprintf (str, sizeof(str), "Attempted to free an unallocated pointer: %p\n", ptr);
mem_abort2 (str,file,line,function);
}
......
......@@ -31,21 +31,21 @@ char *log_open_syslog (const char *str)
{
char *ident,*facility;
if ((ident = mem_alloc (strlen (str) + 1)) == NULL)
char *fac_separator = strchr(str, '.');
if (fac_separator == NULL)
return (NULL);
strcpy (ident,str);
size_t sz = strlen(str) + 1;
if ((ident = mem_alloc (sz)) == NULL)
return (NULL);
if ((facility = strchr (ident,'.')) == NULL)
{
mem_free (ident);
return (NULL);
}
snprintf(ident, sz, "%s", str);
facility = ident + (fac_separator - str);
*facility++ = '\0';
for (i = 0; facilitynames[i].c_name != NULL; i++)
if (!strcmp (facility,list[i].name))
if (strcmp (facility,list[i].name) == 0)
{
openlog (ident,LOG_PID,list[i].c_val);
return (ident);
......
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