Replace all sprintf with snprintf

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-03-05 23:55:15 +08:00 committed by Petro Karashchenko
parent 134b8b538f
commit 7c37421266
48 changed files with 237 additions and 174 deletions

View File

@ -270,22 +270,23 @@ int main(int argc, char *argv[])
/* 29 bit address */
frame.can_id = frame.can_id & ~CAN_EFF_FLAG;
sprintf(sbuf, "T%08" PRIx32 "%d",
frame.can_id, frame.len);
snprintf(sbuf, sizeof(sbuf), "T%08" PRIx32 "%d",
frame.can_id, frame.len);
sbp = &sbuf[10];
}
else
{
/* 11 bit address */
sprintf(sbuf, "t%03" PRIx32 "%d",
frame.can_id, frame.len);
snprintf(sbuf, sizeof(sbuf), "t%03" PRIx32 "%d",
frame.can_id, frame.len);
sbp = &sbuf[5];
}
for (i = 0; i < frame.len; i++)
{
sprintf(sbp, "%02X", frame.data[i]);
snprintf(sbp, sizeof(sbuf) - (sbp - sbuf),
"%02X", frame.data[i]);
sbp += 2;
}

View File

@ -219,7 +219,7 @@ static void el_print_file(const char *workdir)
/* Create full path to log file embedlog will use */
sprintf(log_path, "%s/log-rotate", workdir);
snprintf(log_path, sizeof(log_path), "%s/log-rotate", workdir);
/* Enable file rotation, maximum 5 files will be created, none of the log
* files size shall exceed 512 bytes. Rotate size is low to present how

View File

@ -156,13 +156,14 @@ int main(int argc, FAR char *argv[])
/* Save the sector in our array */
sectors[x] = (uint16_t) logsector;
sectors[x] = (uint16_t)logsector;
seqs[x] = seq++;
/* Now write some data to the sector */
sprintf(buffer, "Logical sector %d sequence %d\n",
sectors[x], seqs[x]);
snprintf(buffer, fmt.availbytes,
"Logical sector %d sequence %d\n",
sectors[x], seqs[x]);
readwrite.logsector = sectors[x];
readwrite.offset = 0;
@ -202,8 +203,9 @@ int main(int argc, FAR char *argv[])
printf("\r%d ", sectors[x]);
sprintf(&buffer[100], "Logical sector %d sequence %d\n",
sectors[x], seqs[x]);
snprintf(&buffer[100], fmt.availbytes - 100,
"Logical sector %d sequence %d\n",
sectors[x], seqs[x]);
if (strcmp(buffer, &buffer[100]) != 0)
{
@ -224,8 +226,9 @@ int main(int argc, FAR char *argv[])
/* Now write over the sector data with new data, causing a relocation.
*/
sprintf(buffer, "Logical sector %d sequence %d\n",
sectors[x], seqs[x]);
snprintf(buffer, fmt.availbytes,
"Logical sector %d sequence %d\n",
sectors[x], seqs[x]);
readwrite.logsector = sectors[x];
readwrite.offset = 0;
readwrite.count = strlen(buffer) + 1;
@ -252,7 +255,8 @@ int main(int argc, FAR char *argv[])
* causing a relocation.
*/
sprintf(buffer, "Appended data in sector %d\n", sectors[x]);
snprintf(buffer, fmt.availbytes,
"Appended data in sector %d\n", sectors[x]);
readwrite.logsector = sectors[x];
readwrite.offset = 64;
readwrite.count = strlen(buffer) + 1;

View File

@ -50,7 +50,8 @@ int foc_device_init(FAR struct foc_device_s *dev, int id)
/* Get FOC devpath */
sprintf(devpath, "%s%d", CONFIG_EXAMPLES_FOC_DEVPATH, id);
snprintf(devpath, sizeof(devpath), "%s%d",
CONFIG_EXAMPLES_FOC_DEVPATH, id);
/* Open FOC device */

View File

@ -887,10 +887,10 @@ int foc_motor_init(FAR struct foc_motor_b16_s *motor,
/* Get qenco devpath */
sprintf(motor->qedpath,
"%s%d",
CONFIG_EXAMPLES_FOC_QENCO_DEVPATH,
motor->envp->id);
snprintf(motor->qedpath, sizeof(motor->qedpath),
"%s%d",
CONFIG_EXAMPLES_FOC_QENCO_DEVPATH,
motor->envp->id);
/* Configure qenco angle handler */
@ -918,10 +918,10 @@ int foc_motor_init(FAR struct foc_motor_b16_s *motor,
/* Get hall devpath */
sprintf(motor->hldpath,
"%s%d",
CONFIG_EXAMPLES_FOC_HALL_DEVPATH,
motor->envp->id);
snprintf(motor->hldpath, sizeof(motor->hldpath),
"%s%d",
CONFIG_EXAMPLES_FOC_HALL_DEVPATH,
motor->envp->id);
/* Configure hall angle handler */

View File

@ -871,10 +871,10 @@ int foc_motor_init(FAR struct foc_motor_f32_s *motor,
/* Get qenco devpath */
sprintf(motor->qedpath,
"%s%d",
CONFIG_EXAMPLES_FOC_QENCO_DEVPATH,
motor->envp->id);
snprintf(motor->qedpath, sizeof(motor->qedpath),
"%s%d",
CONFIG_EXAMPLES_FOC_QENCO_DEVPATH,
motor->envp->id);
/* Configure qenco angle handler */
@ -902,10 +902,10 @@ int foc_motor_init(FAR struct foc_motor_f32_s *motor,
/* Get hall devpath */
sprintf(motor->hldpath,
"%s%d",
CONFIG_EXAMPLES_FOC_HALL_DEVPATH,
motor->envp->id);
snprintf(motor->hldpath, sizeof(motor->hldpath),
"%s%d",
CONFIG_EXAMPLES_FOC_HALL_DEVPATH,
motor->envp->id);
/* Configure hall angle handler */

View File

@ -107,7 +107,7 @@ static FAR void *foc_control_thr(FAR void *arg)
/* Get queue name */
sprintf(mqname, "%s%d", CONTROL_MQ_MQNAME, envp->id);
snprintf(mqname, sizeof(mqname), "%s%d", CONTROL_MQ_MQNAME, envp->id);
/* Open queue */
@ -299,7 +299,7 @@ int foc_ctrlthr_init(FAR struct foc_ctrl_env_s *foc, int i, FAR mqd_t *mqd,
/* Get queue name */
sprintf(mqname, "%s%d", CONTROL_MQ_MQNAME, foc->id);
snprintf(mqname, sizeof(mqname), "%s%d", CONTROL_MQ_MQNAME, foc->id);
/* Initialize thread recv queue */

View File

@ -192,8 +192,9 @@ void parse_and_callback(cJSON *item, const char *prefix)
{
while (item)
{
char *newprefix = malloc(strlen(prefix) + strlen(item->name) + 2);
sprintf(newprefix, "%s/%s", prefix, item->name);
size_t len = strlen(prefix) + strlen(item->name) + 2;
char *newprefix = malloc(len);
snprintf(newprefix, len, "%s/%s", prefix, item->name);
int dorecurse = callback(newprefix, item->type, item);
if (item->child && dorecurse) parse_and_callback(item->child, newprefix);
item = item->next;

View File

@ -207,7 +207,8 @@ static int download_firmware_image(FAR const char *url)
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
{
sprintf(&hash[i * 2], "%02x", digest[i]);
snprintf(&hash[i * 2], sizeof(hash) - i * 2,
"%02x", digest[i]);
}
hash[MD5_HASH_LENGTH] = '\0';

View File

@ -212,7 +212,8 @@ static void show_directories(const char *path, int indent)
{
char *subdir;
printf("%s/\n", direntry->d_name);
sprintf(g_namebuffer, "%s/%s", path, direntry->d_name);
snprintf(g_namebuffer, sizeof(g_namebuffer),
"%s/%s", path, direntry->d_name);
subdir = strdup(g_namebuffer);
show_directories(subdir, indent + 1);
free(subdir);

View File

@ -152,7 +152,7 @@ static int lo_client(void)
for (i = 0; ; i++)
{
sprintf(outbuf, "Loopback message %d", i);
snprintf(outbuf, sizeof(outbuf), "Loopback message %d", i);
len = strlen(outbuf);
printf("lo_client: Sending '%s' (%d bytes)\n", outbuf, len);

View File

@ -92,8 +92,9 @@ static char *padstr(char *s, int length)
static char buf[MAXSTRLEN];
char fmt[10];
sprintf(fmt, (int)strlen(s) > length ? "%%.%ds" : "%%-%ds", length);
sprintf(buf, fmt, s);
snprintf(fmt, sizeof(fmt),
(int)strlen(s) > length ? "%%.%ds" : "%%-%ds", length);
snprintf(buf, sizeof(buf), fmt, s);
return buf;
}
@ -206,7 +207,7 @@ static void idle(void)
}
tp = localtime(&t);
sprintf(buf, " %.2d-%.2d-%.4d %.2d:%.2d:%.2d",
snprintf(buf, sizeof(buf), " %.2d-%.2d-%.4d %.2d:%.2d:%.2d",
tp->tm_mday, tp->tm_mon + 1, tp->tm_year + 1900,
tp->tm_hour, tp->tm_min, tp->tm_sec);

View File

@ -204,7 +204,7 @@ static void showfile(char *fname)
}
else
{
sprintf(buf, "ERROR: file '%s' not found", fname);
snprintf(buf, sizeof(buf), "ERROR: file '%s' not found", fname);
errormsg(buf);
}
}

View File

@ -98,7 +98,7 @@ int main(int argc, char **argv, char **envp)
for (i = 0; ; i++)
{
sprintf(outbuf, "Remote message %d", i);
snprintf(outbuf, sizeof(outbuf), "Remote message %d", i);
len = strlen(outbuf);
printf("client: Sending '%s' (%d bytes)\n", outbuf, len);

View File

@ -163,7 +163,7 @@ int main(int argc, FAR char *argv[])
* from the poll.
*/
sprintf(buffer, "Message %d", count);
snprintf(buffer, sizeof(buffer), "Message %d", count);
nbytes = write(fd1, buffer, strlen(buffer));
if (nbytes < 0)
{

View File

@ -92,7 +92,7 @@ int main(int argc, FAR char *argv[])
sgreen = 1;
}
sprintf(buffer, "#%02X%02X%02X", red, green, blue);
snprintf(buffer, sizeof(buffer), "#%02X%02X%02X", red, green, blue);
write(fd, buffer, 8);
usleep(5000);
}

View File

@ -389,7 +389,8 @@ static void readdirectories(const char *path, struct node_s *entry)
/* Get the full path to the entry */
sprintf(g_scratchbuffer, "%s/%s", path, direntry->d_name);
snprintf(g_scratchbuffer, sizeof(g_scratchbuffer),
"%s/%s", path, direntry->d_name);
fullpath = strdup(g_scratchbuffer);
if (DIRENT_ISDIRECTORY(direntry->d_type))

View File

@ -249,8 +249,9 @@ static void wgetjson_json_item_scan(cJSON *item, const char *prefix)
while (item)
{
const char *string = item->string ? item->string : "(null)";
newprefix = malloc(strlen(prefix) + strlen(string) + 2);
sprintf(newprefix, "%s/%s", prefix, string);
size_t len = strlen(prefix) + strlen(string) + 2;
newprefix = malloc(len);
snprintf(newprefix, len, "%s/%s", prefix, string);
dorecurse = wgetjson_json_item_callback(newprefix, item->type, item);
if (item->child && dorecurse)

View File

@ -262,7 +262,8 @@ WINDOW *Xinitscr(int argc, char *argv[])
def_shell_mode();
sprintf(ttytype, "pdcurses|PDCurses for %s", PDC_sysname());
snprintf(ttytype, sizeof(ttytype),
"pdcurses|PDCurses for %s", PDC_sysname());
return stdscr;
}

View File

@ -184,7 +184,7 @@ static void dstack(char *fmt, int num, PANEL *pan)
{
char s80[80];
sprintf(s80, fmt, num, pan);
snprintf(s80, sizeof(s80), fmt, num, pan);
PDC_LOG(("%s b=%s t=%s", s80, _bottom_panel ? _bottom_panel->user : "--",
_top_panel ? _top_panel->user : "--"));
@ -220,7 +220,7 @@ static void dtouchline(PANEL *pan, int start, int count)
{
char s80[80];
sprintf(s80, "dtouchline s=%d c=%d", start, count);
snprintf(s80, sizeof(s80), "dtouchline s=%d c=%d", start, count);
dpanel(s80, pan);
touchline(pan->win, start, count);
}

View File

@ -179,7 +179,7 @@ static struct Value *hex(struct Value *v, long int value, long int digits)
{
char buf[sizeof(long int) * 2 + 1];
sprintf(buf, "%0*lx", (int)digits, value);
snprintf(buf, sizeof(buf), "%0*lx", (int)digits, value);
Value_new_STRING(v);
String_appendChars(&v->u.string, buf);
return v;
@ -686,8 +686,9 @@ static struct Value *fn_date(struct Value *v, struct Auto *stack)
String_size(&v->u.string, 10);
time(&t);
now = localtime(&t);
sprintf(v->u.string.character, "%02d-%02d-%04d", now->tm_mon + 1,
now->tm_mday, now->tm_year + 1900);
snprintf(v->u.string.character, v->u.string.length + 1,
"%02d-%02d-%04d", now->tm_mon + 1,
now->tm_mday, now->tm_year + 1900);
return v;
}
@ -964,7 +965,7 @@ static struct Value *fn_hexi(struct Value *v, struct Auto *stack)
{
char buf[sizeof(long int) * 2 + 1];
sprintf(buf, "%lx", intValue(stack, 0));
snprintf(buf, sizeof(buf), "%lx", intValue(stack, 0));
Value_new_STRING(v);
String_appendChars(&v->u.string, buf);
return v;
@ -982,7 +983,7 @@ static struct Value *fn_hexd(struct Value *v, struct Auto *stack)
return Value_new_ERROR(v, OUTOFRANGE, _("number"));
}
sprintf(buf, "%lx", n);
snprintf(buf, sizeof(buf), "%lx", n);
Value_new_STRING(v);
String_appendChars(&v->u.string, buf);
return v;
@ -1639,7 +1640,7 @@ static struct Value *fn_oct(struct Value *v, struct Auto *stack)
{
char buf[sizeof(long int) * 3 + 1];
sprintf(buf, "%lo", intValue(stack, 0));
snprintf(buf, sizeof(buf), "%lo", intValue(stack, 0));
Value_new_STRING(v);
String_appendChars(&v->u.string, buf);
return v;
@ -1910,8 +1911,9 @@ static struct Value *fn_times(struct Value *v, struct Auto *stack)
String_size(&v->u.string, 8);
time(&t);
now = localtime(&t);
sprintf(v->u.string.character, "%02d:%02d:%02d", now->tm_hour, now->tm_min,
now->tm_sec);
snprintf(v->u.string.character, v->u.string.length + 1,
"%02d:%02d:%02d", now->tm_hour, now->tm_min,
now->tm_sec);
return v;
}

View File

@ -162,7 +162,8 @@ static void Xref_print(struct Xref *root,
FS_putChars(chn, "\n ");
}
sprintf(buf, " %ld", Program_lineNumber(p, &cur->line));
snprintf(buf, sizeof(buf), " %ld",
Program_lineNumber(p, &cur->line));
FS_putChars(chn, buf);
}
while (cur != tail);
@ -184,7 +185,8 @@ static void printLine(const void *k, struct Program *p, int chn)
{
char buf[80];
sprintf(buf, "%8ld", Program_lineNumber(p, (const struct Pc *)k));
snprintf(buf, sizeof(buf), "%8ld",
Program_lineNumber(p, (const struct Pc *)k));
FS_putChars(chn, buf);
}
@ -603,7 +605,8 @@ void Program_trace(struct Program *this, struct Pc *pc, int dev, int tr)
{
char buf[40];
sprintf(buf, "<%ld>\n", this->code[pc->line]->u.integer);
snprintf(buf, sizeof(buf), "<%ld>\n",
this->code[pc->line]->u.integer);
FS_putChars(dev, buf);
}
}

View File

@ -2746,7 +2746,7 @@ static FAR char *strstring(void)
x = expr();
match(CPAREN);
sprintf(g_iobuffer, "%g", x);
snprintf(g_iobuffer, sizeof(g_iobuffer), "%g", x);
answer = mystrdup(g_iobuffer);
if (!answer)
{

View File

@ -332,7 +332,7 @@ static int chat_internalise(FAR struct chat *priv,
if (rhs)
{
len = strlen(tok->string);
len = strlen(tok->string) + 1;
if (!tok->no_termin)
{
/* Add space for the line terminator */
@ -340,13 +340,13 @@ static int chat_internalise(FAR struct chat *priv,
len += 2;
}
line->rhs = malloc(len + 1);
line->rhs = malloc(len);
if (line->rhs)
{
/* Copy the token and add the line terminator as appropriate */
sprintf(line->rhs, tok->no_termin ? "%s" : "%s\r\n",
tok->string);
snprintf(line->rhs, len,
tok->no_termin ? "%s" : "%s\r\n", tok->string);
}
else
{

View File

@ -399,7 +399,7 @@ char *md5_hash(const uint8_t * addr, const size_t len)
md5_sum(addr, len, digest);
for (i = 0; i < 16; i++)
{
sprintf(&hash[i * 2], "%02x", digest[i]);
snprintf(&hash[i * 2], 33 - i * 2, "%02x", digest[i]);
}
hash[32] = 0;

View File

@ -1383,8 +1383,9 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node,
FAR struct ftpd_pathnode_s *node1;
FAR struct ftpd_pathnode_s *node2;
FAR char *path;
FAR size_t allocsize;
FAR size_t namelen;
size_t allocsize;
size_t namelen;
size_t next;
if (node == NULL)
{
@ -1424,7 +1425,7 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node,
}
else
{
allocsize += namelen +1;
allocsize += namelen + 1;
}
}
else
@ -1441,7 +1442,7 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node,
return NULL;
}
allocsize = 0;
next = 0;
node1 = node;
while (node1 != NULL)
{
@ -1471,19 +1472,20 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node,
{
if (namelen <= 0)
{
allocsize += sprintf(&path[allocsize], "/");
snprintf(&path[next], allocsize - next, "/");
}
else
{
allocsize += sprintf(&path[allocsize], "%s", node1->name);
snprintf(&path[next], allocsize - next, "%s", node1->name);
}
}
else
{
allocsize += sprintf(&path[allocsize], "%s%s", node1->name, "/");
snprintf(&path[next], allocsize - next, "%s%s", node1->name, "/");
}
node1 = node1->flink;
next += strlen(&path[next]);
}
return path;

View File

@ -153,8 +153,8 @@ int tftpget_cb(FAR const char *remote, in_addr_t addr, bool binary,
if (blockno == 1)
{
len = tftp_mkreqpacket(packet, TFTP_RRQ, remote,
binary);
len = tftp_mkreqpacket(packet, TFTP_IOBUFSIZE,
TFTP_RRQ, remote, binary);
server.sin_port = HTONS(CONFIG_NETUTILS_TFTP_PORT);
ret = tftp_sendto(sd, packet, len, &server);
if (ret != len)

View File

@ -158,7 +158,7 @@
/* Defined in tftp_packet.c *************************************************/
extern int tftp_sockinit(struct sockaddr_in *server, in_addr_t addr);
extern int tftp_mkreqpacket(uint8_t *buffer, int opcode,
extern int tftp_mkreqpacket(uint8_t *buffer, size_t len, int opcode,
const char *path, bool binary);
extern int tftp_mkackpacket(uint8_t *buffer, uint16_t blockno);
extern int tftp_mkerrpacket(uint8_t *buffer, uint16_t errorcode,

View File

@ -122,13 +122,16 @@ int tftp_sockinit(struct sockaddr_in *server, in_addr_t addr)
*
****************************************************************************/
int tftp_mkreqpacket(uint8_t *buffer, int opcode, const char *path,
bool binary)
int tftp_mkreqpacket(uint8_t *buffer, size_t len, int opcode,
const char *path, bool binary)
{
int ret;
buffer[0] = opcode >> 8;
buffer[1] = opcode & 0xff;
return sprintf((char *)&buffer[2], "%s%c%s", path, 0,
ret = snprintf((char *)&buffer[2], len - 2, "%s%c%s", path, 0,
tftp_mode(binary)) + 3;
return ret < len ? ret : len;
}
/****************************************************************************

View File

@ -302,7 +302,8 @@ int tftpput_cb(FAR const char *remote, in_addr_t addr, bool binary,
retry = 0;
for (; ; )
{
packetlen = tftp_mkreqpacket(packet, TFTP_WRQ, remote, binary);
packetlen = tftp_mkreqpacket(packet, TFTP_IOBUFSIZE,
TFTP_WRQ, remote, binary);
ret = tftp_sendto(sd, packet, packetlen, &server);
if (ret != packetlen)
{

View File

@ -179,14 +179,15 @@ int main(int argc, char *argv[])
path_info = getenv("PATH_INFO");
if (path_info)
{
cp = (char *)malloc(strlen(script_name) + strlen(path_info) + 1);
size_t len = strlen(script_name) + strlen(path_info) + 1;
cp = (char *)malloc(len);
if (!cp)
{
internal_error("Out of memory.");
return 2;
}
sprintf(cp, "%s%s", script_name, path_info);
snprintf(cp, len, "%s%s", script_name, path_info);
script_name = cp;
}

View File

@ -324,7 +324,8 @@ static int check_filename(char *filename)
*cp = '\0';
}
authname = malloc(strlen(dirname) + 1 + sizeof(CONFIG_AUTH_FILE));
fnl = strlen(dirname) + 1 + sizeof(CONFIG_AUTH_FILE);
authname = malloc(fnl);
if (!authname)
{
/* out of memory */
@ -333,7 +334,7 @@ static int check_filename(char *filename)
return 0;
}
sprintf(authname, "%s/%s", dirname, CONFIG_AUTH_FILE);
snprintf(authname, fnl, "%s/%s", dirname, CONFIG_AUTH_FILE);
r = stat(authname, &sb);
free(dirname);
@ -907,6 +908,7 @@ int main(int argc, char *argv[])
char *script_name;
char *path_info;
char *path_translated;
size_t len;
int errcode = 0;
/* Default formats. */
@ -935,14 +937,15 @@ int main(int argc, char *argv[])
path_info = "";
}
g_url = (char *)malloc(strlen(script_name) + strlen(path_info) + 1);
len = strlen(script_name) + strlen(path_info) + 1;
g_url = (char *)malloc(len);
if (!g_url)
{
internal_error("Out of memory.");
return 2;
}
sprintf(g_url, "%s%s", script_name, path_info);
snprintf(g_url, len, "%s%s", script_name, path_info);
/* Get the name of the file to parse. */

View File

@ -178,7 +178,7 @@ void httpd_strencode(char *to, int tosize, char *from)
}
else
{
sprintf(to, "%%%02x", (int)*from & 0xff);
snprintf(to, tosize - tolen, "%%%02x", (int)*from & 0xff);
to += 3;
tolen += 3;
}

View File

@ -1770,7 +1770,7 @@ int webclient_perform(FAR struct webclient_context *ctx)
char post_size[sizeof("18446744073709551615")];
dest = append(dest, ep, g_httpcontsize);
sprintf(post_size, "%zu", ctx->bodylen);
snprintf(post_size, sizeof(post_size), "%zu", ctx->bodylen);
dest = append(dest, ep, post_size);
dest = append(dest, ep, g_httpcrnl);
}

View File

@ -164,6 +164,7 @@ int xmlrpc_getstring(struct xmlrpc_s *xmlcall, char *arg)
int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
{
va_list argp;
int next = 0;
int index = 0;
int close = 0;
int isstruct = 0;
@ -195,6 +196,7 @@ int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
sizeof(xmlcall->response));
}
next = strlen(xmlcall->response);
va_start(argp, args);
while (args[index])
@ -203,10 +205,15 @@ int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
{
if ((args[index] != '{') && (args[index] != '}'))
{
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" <member>\n");
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" <name>%s</name>\n", va_arg(argp, char *));
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <member>\n");
next += strlen(&xmlcall->response[next]);
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <name>%s</name>\n",
va_arg(argp, char *));
next += strlen(&xmlcall->response[next]);
close = 1;
}
}
@ -214,39 +221,45 @@ int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
switch (args[index])
{
case '{':
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" <value><struct>\n");
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <value><struct>\n");
isstruct = 1;
break;
case '}':
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" </struct></value>\n");
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" </struct></value>\n");
isstruct = 0;
break;
case 'i':
i = va_arg(argp, int);
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" <value><int>%d</int></value>\r\n", i);
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <value><int>%d</int></value>\r\n", i);
break;
case 'b':
i = va_arg(argp, int);
sprintf(&xmlcall->response[strlen(xmlcall->response)],
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <value><boolean>%d</boolean></value>\r\n", i);
break;
case 'd':
d = va_arg(argp, double);
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" <value><double>%f</double></value>\r\n", d);
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <value><double>%f</double></value>\r\n", d);
break;
case 's':
s = va_arg(argp, char *);
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" <value><string>%s</string></value>\r\n", s);
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" <value><string>%s</string></value>\r\n", s);
break;
default:
@ -254,10 +267,13 @@ int xmlrpc_buildresponse(struct xmlrpc_s *xmlcall, char *args, ...)
break;
}
next += strlen(&xmlcall->response[next]);
if (close)
{
sprintf(&xmlcall->response[strlen(xmlcall->response)],
" </member>\n");
snprintf(&xmlcall->response[next],
sizeof(xmlcall->response) - next,
" </member>\n");
next += strlen(&xmlcall->response[next]);
close = 0;
}

View File

@ -297,19 +297,22 @@ void nsh_dumpbuffer(FAR struct nsh_vtbl_s *vtbl, FAR const char *msg,
nsh_output(vtbl, "%s:\n", msg);
for (i = 0; i < nbytes; i += 16)
{
sprintf(line, "%04x: ", i);
snprintf(line, sizeof(line), "%04x: ", i);
size = strlen(line);
for (j = 0; j < 16; j++)
{
size = strlen(line);
if (i + j < nbytes)
{
sprintf(&line[size], "%02x ", buffer[i + j]);
snprintf(&line[size], sizeof(line) - size,
"%02x ", buffer[i + j]);
}
else
{
strlcpy(&line[size], " ", sizeof(line) - size);
}
size += strlen(&line[size]);
}
for (j = 0; j < 16; j++)
@ -317,8 +320,9 @@ void nsh_dumpbuffer(FAR struct nsh_vtbl_s *vtbl, FAR const char *msg,
if (i + j < nbytes)
{
ch = buffer[i + j];
sprintf(&line[strlen(line)], "%c",
ch >= 0x20 && ch <= 0x7e ? ch : '.');
snprintf(&line[size], sizeof(line) - size,
"%c", ch >= 0x20 && ch <= 0x7e ? ch : '.');
size += strlen(&line[size]);
}
}

View File

@ -283,7 +283,7 @@ int nsh_setvar(FAR struct nsh_vtbl_s *vtbl, FAR const char *name,
/* Now, put the new name=value string into the NSH variable buffer */
sprintf(pair, "%s=%s", name, value);
snprintf(pair, varlen, "%s=%s", name, value);
return OK;
}
#endif

View File

@ -562,9 +562,11 @@ static void cfgdatacmd_show_all_config_items(void)
/* Print header */
#ifdef CONFIG_MTD_CONFIG_NAMED
sprintf(fmtstr, "%%-%ds%%-6sData\n", CONFIG_MTD_CONFIG_NAME_LEN);
snprintf(fmtstr, sizeof(fmtstr),
"%%-%ds%%-6sData\n", CONFIG_MTD_CONFIG_NAME_LEN);
printf(fmtstr, "Name", "Len");
sprintf(fmtstr, "%%-%ds%%-6d", CONFIG_MTD_CONFIG_NAME_LEN);
snprintf(fmtstr, sizeof(fmtstr),
"%%-%ds%%-6d", CONFIG_MTD_CONFIG_NAME_LEN);
#else
strlcpy(fmtstr, "%-6s%-6s%-6sData\n", sizeof(fmtstr));
printf(fmtstr, "ID", "Inst", "Len");
@ -618,7 +620,8 @@ static void cfgdatacmd_show_all_config_items(void)
char fmtstr2[10];
#ifdef CONFIG_MTD_CONFIG_NAMED
sprintf(fmtstr2, "\n%ds", CONFIG_MTD_CONFIG_NAME_LEN + 6);
snprintf(fmtstr2, sizeof(fmtstr2),
"\n%ds", CONFIG_MTD_CONFIG_NAME_LEN + 6);
#else
strlcpy(fmtstr2, "\n%18s", sizeof(fmtstr2));
#endif

View File

@ -45,7 +45,7 @@
#define NTP_TIME_STR_MAX_LEN (1 + 21 + 1 + 9 + 1)
static void
format_ntptimestamp(int64_t ts, FAR char *buf)
format_ntptimestamp(int64_t ts, FAR char *buf, size_t len)
{
FAR const char *sign;
uint64_t absts;
@ -61,9 +61,9 @@ format_ntptimestamp(int64_t ts, FAR char *buf)
absts = ts;
}
sprintf(buf, "%s%" PRIu64 ".%09" PRIu64,
sign, absts >> 32,
((absts & 0xffffffff) * NSEC_PER_SEC) >> 32);
snprintf(buf, len, "%s%" PRIu64 ".%09" PRIu64,
sign, absts >> 32,
((absts & 0xffffffff) * NSEC_PER_SEC) >> 32);
}
/****************************************************************************
@ -115,8 +115,10 @@ int main(int argc, FAR char *argv[])
}
#endif
format_ntptimestamp(status.samples[i].offset, offset_buf);
format_ntptimestamp(status.samples[i].delay, delay_buf);
format_ntptimestamp(status.samples[i].offset,
offset_buf, sizeof(offset_buf));
format_ntptimestamp(status.samples[i].delay,
delay_buf, sizeof(delay_buf));
printf("[%u] srv %s offset %s delay %s\n",
i, name, offset_buf, delay_buf);
}

View File

@ -729,7 +729,9 @@ static void dump_notes(size_t nread)
for (i = 0; i < count; i++)
{
ret += sprintf(&out[ret], " 0x%x", note_binary->nbi_data[i]);
snprintf(&out[ret], sizeof(out) - ret,
" 0x%x", note_binary->nbi_data[i]);
ret += strlen(&out[ret]);
}
trace_dump_unflatten(&ip, note_binary->nbi_ip,

View File

@ -917,9 +917,9 @@ static int tcurses_vt100_setcolors(FAR struct termcurses_s *dev,
if ((colors->color_mask & TCURS_COLOR_FG) != 0)
{
sprintf(str, g_setfgcolor,
tcurses_vt100_getcolorindex(colors->fg_red, colors->fg_green,
colors->fg_blue));
snprintf(str, sizeof(str), g_setfgcolor,
tcurses_vt100_getcolorindex(colors->fg_red, colors->fg_green,
colors->fg_blue));
ret = write(fd, str, strlen(str));
}
@ -932,9 +932,9 @@ static int tcurses_vt100_setcolors(FAR struct termcurses_s *dev,
colors->bg_red = 0;
}
sprintf(str, g_setbgcolor,
tcurses_vt100_getcolorindex(colors->bg_red, colors->bg_green,
colors->bg_blue));
snprintf(str, sizeof(str), g_setbgcolor,
tcurses_vt100_getcolorindex(colors->bg_red, colors->bg_green,
colors->bg_blue));
ret = write(fd, str, strlen(str));
}

View File

@ -228,6 +228,7 @@ int main(int argc, FAR char *argv[])
{
uint8_t uniqueid[CONFIG_BOARDCTL_UNIQUEID_SIZE];
FAR char *formatter;
size_t len;
int i;
struct cfg_s cfg =
@ -258,8 +259,9 @@ int main(int argc, FAR char *argv[])
return -1;
}
formatter = malloc(strlen(cfg.format) + 2);
sprintf(formatter, "%%%s", cfg.format);
len = strlen(cfg.format) + 2;
formatter = malloc(len);
snprintf(formatter, len, "%%%s", cfg.format);
if (cfg.prefix != NULL)
{

View File

@ -128,8 +128,8 @@ static int pubsubtest_thread_entry(int argc, FAR char *argv[])
char fname[32];
FAR FILE *f;
sprintf(fname, CONFIG_UORB_SRORAGE_DIR"/uorb_timings%u.txt",
timingsgroup);
snprintf(fname, sizeof(fname),
CONFIG_UORB_SRORAGE_DIR"/uorb_timings%u.txt", timingsgroup);
f = fopen(fname, "w");
if (f == NULL)

View File

@ -24,6 +24,7 @@
#include <nuttx/config.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <stdarg.h>
@ -758,7 +759,7 @@ static void vi_setcursor(FAR struct vi_s *vi, uint16_t row, uint16_t column)
/* Send the VT100 CURSORPOS command */
vi_write(vi, buffer, len);
vi_write(vi, buffer, MIN(len, sizeof(buffer)));
}
/****************************************************************************
@ -858,10 +859,13 @@ static void vi_printf(FAR struct vi_s *vi, FAR const char *prefix,
/* Expand the prefix message in the scratch buffer */
len = prefix ? snprintf(vi->scratch, SCRATCH_BUFSIZE, "%s", prefix) : 0;
len = prefix ? snprintf(vi->scratch,
sizeof(vi->scratch), "%s", prefix) : 0;
len = MIN(len, sizeof(vi->scratch));
va_start(ap, fmt);
len += vsnprintf(vi->scratch + len, SCRATCH_BUFSIZE - len, fmt, ap);
len += vsnprintf(vi->scratch + len, sizeof(vi->scratch) - len, fmt, ap);
len = MIN(len, sizeof(vi->scratch));
vvidbg(fmt, ap);
va_end(ap);
@ -1299,8 +1303,8 @@ static bool vi_savetext(FAR struct vi_s *vi, FAR const char *filename,
fclose(stream);
len = sprintf(vi->scratch, "%dC written", nwritten);
vi_write(vi, vi->scratch, len);
len = snprintf(vi->scratch, sizeof(vi->scratch), "%dC written", nwritten);
vi_write(vi, vi->scratch, MIN(len, sizeof(vi->scratch)));
return true;
}
@ -1955,10 +1959,10 @@ static void vi_showlinecol(FAR struct vi_s *vi)
vi_cursoroff(vi);
vi_setcursor(vi, vi->display.row - 1, vi->display.column - 15);
len = snprintf(vi->scratch, SCRATCH_BUFSIZE, "%jd,%d",
len = snprintf(vi->scratch, sizeof(vi->scratch), "%jd,%d",
(uintmax_t)(vi->cursor.row + vi->vscroll + 1),
vi->cursor.column + vi->hscroll + 1);
vi_write(vi, vi->scratch, len);
vi_write(vi, vi->scratch, MIN(len, sizeof(vi->scratch)));
vi_clrtoeol(vi);
vi_cursoron(vi);

View File

@ -705,7 +705,7 @@ static int zms_sendzsinit(FAR struct zm_state_s *pzm)
static int zms_sendfilename(FAR struct zm_state_s *pzm)
{
FAR struct zms_state_s *pzms = (FAR struct zms_state_s *)pzm;
FAR uint8_t *ptr = pzm->scratch;
FAR char *ptr = (FAR char *)pzm->scratch;
int len;
int ret;
@ -768,19 +768,19 @@ static int zms_sendfilename(FAR struct zm_state_s *pzm)
*/
#ifdef CONFIG_SYSTEM_ZMODEM_TIMESTAMPS
sprintf((FAR char *)ptr, "%ld %lo 0 %d 1 %ld 0",
(unsigned long)pzms->filesize, (unsigned long)pzms->timestamp,
CONFIG_SYSTEM_ZMODEM_SERIALNO, (unsigned long)pzms->filesize);
snprintf(ptr, sizeof(pzm->scratch), "%ld %lo 0 %d 1 %ld 0",
(unsigned long)pzms->filesize, (unsigned long)pzms->timestamp,
CONFIG_SYSTEM_ZMODEM_SERIALNO, (unsigned long)pzms->filesize);
#else
sprintf((FAR char *)ptr, "%ld 0 0 %d 1 %ld 0",
(unsigned long)pzms->filesize, CONFIG_SYSTEM_ZMODEM_SERIALNO,
(unsigned long)pzms->filesize);
snprintf(ptr, sizeof(pzm->scratch), "%ld 0 0 %d 1 %ld 0",
(unsigned long)pzms->filesize, CONFIG_SYSTEM_ZMODEM_SERIALNO,
(unsigned long)pzms->filesize);
#endif
ptr += strlen((FAR char *)ptr);
ptr += strlen(ptr);
*ptr++ = '\0';
len = ptr - pzm->scratch;
len = ptr - (FAR char *)pzm->scratch;
DEBUGASSERT(len < CONFIG_SYSTEM_ZMODEM_SNDBUFSIZE);
return zm_senddata(pzm, pzm->scratch, len);
}

View File

@ -859,7 +859,7 @@ static void test_nvs_gc(struct mtdnvs_ctx_s *ctx)
/* 4 byte key */
sprintf(data.name, "k%02d", id);
snprintf(data.name, sizeof(data.name), "k%02d", id);
data.configdata = buf;
data.len = sizeof(buf);
@ -876,7 +876,7 @@ static void test_nvs_gc(struct mtdnvs_ctx_s *ctx)
{
/* 4 byte key */
sprintf(data.name, "k%02d", id);
snprintf(data.name, sizeof(data.name), "k%02d", id);
data.configdata = rd_buf;
data.len = sizeof(rd_buf);
@ -929,7 +929,7 @@ static void test_nvs_gc(struct mtdnvs_ctx_s *ctx)
{
/* 4 byte key */
sprintf(data.name, "k%02d", id);
snprintf(data.name, sizeof(data.name), "k%02d", id);
data.configdata = rd_buf;
data.len = sizeof(rd_buf);
@ -1006,7 +1006,7 @@ static int write_content(uint16_t max_id, uint16_t begin, uint16_t end)
/* 4 byte key */
sprintf(data.name, "k%02d", id);
snprintf(data.name, sizeof(data.name), "k%02d", id);
data.configdata = buf;
data.len = sizeof(buf);
@ -1054,7 +1054,7 @@ static int check_content(uint16_t max_id)
{
/* 4 byte key */
sprintf(data.name, "k%02d", id);
snprintf(data.name, sizeof(data.name), "k%02d", id);
data.configdata = rd_buf;
data.len = sizeof(rd_buf);
@ -1512,7 +1512,7 @@ static void test_nvs_full_sector(struct mtdnvs_ctx_s *ctx)
while (1)
{
sprintf(data.name, "k%04x", filling_id);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id);
data.configdata = (FAR uint8_t *)&filling_id;
data.len = sizeof(filling_id);
@ -1533,7 +1533,7 @@ static void test_nvs_full_sector(struct mtdnvs_ctx_s *ctx)
/* check whether can delete whatever from full storage */
sprintf(data.name, "k%04x", 1);
snprintf(data.name, sizeof(data.name), "k%04x", 1);
data.configdata = NULL;
data.len = 0;
@ -1565,7 +1565,7 @@ static void test_nvs_full_sector(struct mtdnvs_ctx_s *ctx)
goto test_fail;
}
sprintf(data.name, "k%04x", filling_id);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id);
data.configdata = (FAR uint8_t *)&filling_id;
data.len = sizeof(filling_id);
@ -1581,7 +1581,7 @@ static void test_nvs_full_sector(struct mtdnvs_ctx_s *ctx)
for (i = 0; i <= filling_id; i++)
{
sprintf(data.name, "k%04x", i);
snprintf(data.name, sizeof(data.name), "k%04x", i);
data.configdata = (FAR uint8_t *)&data_read;
data.len = sizeof(data_read);
@ -1997,7 +1997,7 @@ static void test_nvs_gc_touched_deleted_ate(struct mtdnvs_ctx_s *ctx)
while (1)
{
sprintf(data.name, "k%04x", filling_id);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id);
data.configdata = (FAR uint8_t *)&filling_id;
data.len = sizeof(filling_id);
@ -2032,7 +2032,7 @@ static void test_nvs_gc_touched_deleted_ate(struct mtdnvs_ctx_s *ctx)
* B(deleted) A gc
*/
sprintf(data.name, "k%04x", filling_id - 1);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id - 1);
data.configdata = NULL;
data.len = 0;
ret = ioctl(fd, CFGDIOC_DELCONFIG, &data);
@ -2049,7 +2049,7 @@ static void test_nvs_gc_touched_deleted_ate(struct mtdnvs_ctx_s *ctx)
*/
filling_id -= 1;
sprintf(data.name, "k%04x", filling_id);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id);
data.configdata = (FAR uint8_t *)&filling_id;
data.len = sizeof(filling_id);
ret = ioctl(fd, CFGDIOC_SETCONFIG, &data);
@ -2064,7 +2064,7 @@ static void test_nvs_gc_touched_deleted_ate(struct mtdnvs_ctx_s *ctx)
for (i = 0; i <= filling_id; i++)
{
sprintf(data.name, "k%04x", i);
snprintf(data.name, sizeof(data.name), "k%04x", i);
data.configdata = (FAR uint8_t *)&data_read;
data.len = sizeof(data_read);
@ -2146,7 +2146,7 @@ static void test_nvs_gc_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
while (1)
{
sprintf(data.name, "k%04x", filling_id);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id);
data.configdata = (FAR uint8_t *)&filling_id;
data.len = sizeof(filling_id);
ret = ioctl(fd, CFGDIOC_SETCONFIG, &data);
@ -2170,7 +2170,7 @@ static void test_nvs_gc_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
* B A(deleted) gc
*/
sprintf(data.name, "k%04x", 1);
snprintf(data.name, sizeof(data.name), "k%04x", 1);
data.configdata = NULL;
data.len = 0;
ret = ioctl(fd, CFGDIOC_DELCONFIG, &data);
@ -2207,7 +2207,7 @@ static void test_nvs_gc_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
*/
update_id = 3;
sprintf(data.name, "k%04x", 2);
snprintf(data.name, sizeof(data.name), "k%04x", 2);
data.configdata = (FAR uint8_t *)&update_id;
data.len = sizeof(update_id);
ret = ioctl(fd, CFGDIOC_SETCONFIG, &data);
@ -2222,7 +2222,7 @@ static void test_nvs_gc_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
for (i = 0; i <= filling_id - 1; i++)
{
sprintf(data.name, "k%04x", i);
snprintf(data.name, sizeof(data.name), "k%04x", i);
data.configdata = (FAR uint8_t *)&data_read;
data.len = sizeof(data_read);
ret = ioctl(fd, CFGDIOC_GETCONFIG, &data);
@ -2323,7 +2323,7 @@ static void test_nvs_gc_not_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
while (1)
{
sprintf(data.name, "k%04x", filling_id);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id);
data.configdata = (FAR uint8_t *)&filling_id;
data.len = sizeof(filling_id);
ret = ioctl(fd, CFGDIOC_SETCONFIG, &data);
@ -2347,7 +2347,7 @@ static void test_nvs_gc_not_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
* B(deleted) A gc
*/
sprintf(data.name, "k%04x", filling_id - 1);
snprintf(data.name, sizeof(data.name), "k%04x", filling_id - 1);
data.configdata = NULL;
data.len = 0;
ret = ioctl(fd, CFGDIOC_DELCONFIG, &data);
@ -2364,7 +2364,7 @@ static void test_nvs_gc_not_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
*/
update_id = 3;
sprintf(data.name, "k%04x", 2);
snprintf(data.name, sizeof(data.name), "k%04x", 2);
data.configdata = (FAR uint8_t *)&update_id;
data.len = sizeof(update_id);
ret = ioctl(fd, CFGDIOC_SETCONFIG, &data);
@ -2379,7 +2379,7 @@ static void test_nvs_gc_not_touched_expired_ate(struct mtdnvs_ctx_s *ctx)
for (i = 0; i <= filling_id - 1; i++)
{
sprintf(data.name, "k%04x", i);
snprintf(data.name, sizeof(data.name), "k%04x", i);
data.configdata = (FAR uint8_t *)&data_read;
data.len = sizeof(data_read);
ret = ioctl(fd, CFGDIOC_GETCONFIG, &data);

View File

@ -1126,7 +1126,7 @@ int main(int argc, FAR char *argv[])
{
fscanf(fp, "%s", s2);
fscanf(fp, "%2c", s3);
sprintf(s1, "%s%s", s2, s3);
snprintf(s1, sizeof(s1), "%s%s", s2, s3);
if (strcmp(s1, teststring) != 0)
{

View File

@ -91,7 +91,8 @@ static int smart_create_test_file(char *filename)
{
g_line_pos[x] = ftell(fd);
sprintf(string, "This is line %d at offset %d\n", x, g_line_pos[x]);
snprintf(string, sizeof(string),
"This is line %d at offset %d\n", x, g_line_pos[x]);
g_line_len[x] = strlen(string);
fprintf(fd, "%s", string);
@ -148,8 +149,9 @@ static int smart_seek_test(char *filename)
fread(readstring, 1, g_line_len[index], fd);
readstring[g_line_len[index]] = '\0';
sprintf(cmpstring, "This is line %d at offset %d\n",
index, g_line_pos[index]);
snprintf(cmpstring, sizeof(cmpstring),
"This is line %d at offset %d\n",
index, g_line_pos[index]);
if (strcmp(readstring, cmpstring) != 0)
{