Fix -Werror=nonnull-compare and -Werror=format-truncation=

Error: module/mod_insmod.c:203:3: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation]
  203 |   strncpy(modp->modname, modname, MODLIB_NAMEMAX);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

wqueue/kwork_thread.c: In function 'work_start_lowpri':
Error: wqueue/kwork_thread.c:212:22: error: '%lx' directive output may be truncated writing between 1 and 16 bytes into a region of size 14 [-Werror=format-truncation=]
  212 |   snprintf(args, 16, "0x%" PRIxPTR, (uintptr_t)wqueue);

local/local_sockif.c: In function 'local_getsockname':
Error: local/local_sockif.c:392:11: error: 'strncpy' specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
  392 |           strncpy(unaddr->sun_path, conn->lc_path, namelen);
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

chip/esp32_wifi_utils.c: In function 'esp_wifi_scan_event_parse':
Error: chip/esp32_wifi_utils.c:373:37: error: argument to 'sizeof' in 'memset' call is the same expression as the destination; did you mean to dereference it? [-Werror=sizeof-pointer-memaccess]
   memset(ap_list_buffer, 0x0, sizeof(ap_list_buffer));
                                     ^

stdio/lib_fputs.c: In function 'fputs':
Error: stdio/lib_fputs.c:99:9: error: nonnull argument 's' compared to NULL [-Werror=nonnull-compare]
   if (s == NULL || stream == NULL)
         ^
Error: stdio/lib_fputs.c:99:27: error: nonnull argument 'stream' compared to NULL [-Werror=nonnull-compare]
   if (s == NULL || stream == NULL)
                           ^

stdio/lib_vfprintf.c: In function 'vfprintf':
Error: stdio/lib_vfprintf.c:40:6: error: nonnull argument 'stream' compared to NULL [-Werror=nonnull-compare]
   if (stream)
      ^

string/lib_strdup.c: In function 'strdup':
Error: string/lib_strdup.c:39:6: error: nonnull argument 's' compared to NULL [-Werror=nonnull-compare]
   if (s)
      ^

string/lib_strndup.c: In function 'strndup':
Error: string/lib_strndup.c:56:6: error: nonnull argument 's' compared to NULL [-Werror=nonnull-compare]
   if (s)
      ^

string/lib_strpbrk.c: In function 'strpbrk':
Error: string/lib_strpbrk.c:39:7: error: nonnull argument 'str' compared to NULL [-Werror=nonnull-compare]
   if (!str || !charset)
       ^~~~
Error: string/lib_strpbrk.c:39:15: error: nonnull argument 'charset' compared to NULL [-Werror=nonnull-compare]
   if (!str || !charset)
               ^~~~~~~~

string/lib_strrchr.c: In function 'strrchr':
Error: string/lib_strrchr.c:40:6: error: nonnull argument 's' compared to NULL [-Werror=nonnull-compare]
   if (s)
      ^

Error: time/lib_asctimer.c:73:50: error: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size between 0 and 12 [-Werror=format-truncation=]
   snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
                                                  ^~
time/lib_asctimer.c:73:21: note: directive argument in the range [-2147481748, 2147483647]
   snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
time/lib_asctimer.c:73:3: note: 'snprintf' output between 17 and 68 bytes into a destination of size 26
   snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            g_wday_name[tp->tm_wday], g_mon_name[tp->tm_mon],
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec,
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            1900 + tp->tm_year);
            ~~~~~~~~~~~~~~~~~~~

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-02-13 07:18:04 +08:00 committed by Xiang Xiao
parent 572f961136
commit 4a16cf71f9
13 changed files with 55 additions and 98 deletions

View file

@ -362,7 +362,7 @@ void esp_wifi_scan_event_parse(void)
return; return;
} }
ap_list_buffer = kmm_malloc(bss_total * sizeof(wifi_ap_record_t)); ap_list_buffer = kmm_zalloc(bss_total * sizeof(wifi_ap_record_t));
if (ap_list_buffer == NULL) if (ap_list_buffer == NULL)
{ {
priv->scan_status = ESP_SCAN_DONE; priv->scan_status = ESP_SCAN_DONE;
@ -370,7 +370,6 @@ void esp_wifi_scan_event_parse(void)
return; return;
} }
memset(ap_list_buffer, 0x0, sizeof(ap_list_buffer));
if (esp_wifi_scan_get_ap_records(&bss_total, if (esp_wifi_scan_get_ap_records(&bss_total,
(wifi_ap_record_t *)ap_list_buffer) == OK) (wifi_ap_record_t *)ap_list_buffer) == OK)
{ {

View file

@ -51,16 +51,6 @@ int fputs(FAR const IPTR char *s, FAR FILE *stream)
int ret; int ret;
char ch; char ch;
/* Make sure that a string was provided. */
#ifdef CONFIG_DEBUG_FEATURES /* Most parameter checking is disabled if DEBUG is off */
if (!s)
{
set_errno(EINVAL);
return EOF;
}
#endif
/* Write the string. Loop until the null terminator is encountered */ /* Write the string. Loop until the null terminator is encountered */
for (nput = 0, ch = up_romgetc(s); ch; nput++, s++, ch = up_romgetc(s)) for (nput = 0, ch = up_romgetc(s); ch; nput++, s++, ch = up_romgetc(s))
@ -93,16 +83,6 @@ int fputs(FAR const IPTR char *s, FAR FILE *stream)
{ {
int nput; int nput;
/* Make sure that a string was provided. */
#ifdef CONFIG_DEBUG_FEATURES /* Most parameter checking is disabled if DEBUG is off */
if (s == NULL || stream == NULL)
{
set_errno(EINVAL);
return EOF;
}
#endif
/* If line buffering is enabled, then we will have to output one character /* If line buffering is enabled, then we will have to output one character
* at a time, checking for a newline character each time. * at a time, checking for a newline character each time.
*/ */

View file

@ -37,23 +37,20 @@ int vfprintf(FAR FILE *stream, FAR const IPTR char *fmt, va_list ap)
struct lib_stdoutstream_s stdoutstream; struct lib_stdoutstream_s stdoutstream;
int n = ERROR; int n = ERROR;
if (stream) /* Wrap the stream in a stream object and let lib_vsprintf
{ * do the work.
/* Wrap the stream in a stream object and let lib_vsprintf */
* do the work.
*/
lib_stdoutstream(&stdoutstream, stream); lib_stdoutstream(&stdoutstream, stream);
/* Hold the stream semaphore throughout the lib_vsprintf /* Hold the stream semaphore throughout the lib_vsprintf
* call so that this thread can get its entire message out * call so that this thread can get its entire message out
* before being pre-empted by the next thread. * before being pre-empted by the next thread.
*/ */
lib_take_semaphore(stream); lib_take_semaphore(stream);
n = lib_vsprintf(&stdoutstream.public, fmt, ap); n = lib_vsprintf(&stdoutstream.public, fmt, ap);
lib_give_semaphore(stream); lib_give_semaphore(stream);
}
return n; return n;
} }

View file

@ -49,17 +49,14 @@ FAR void *memchr(FAR const void *s, int c, size_t n)
{ {
FAR const unsigned char *p = (FAR const unsigned char *)s; FAR const unsigned char *p = (FAR const unsigned char *)s;
if (s) while (n--)
{ {
while (n--) if (*p == (unsigned char)c)
{ {
if (*p == (unsigned char)c) return (FAR void *)p;
{
return (FAR void *)p;
}
p++;
} }
p++;
} }
return NULL; return NULL;

View file

@ -48,19 +48,16 @@
#undef strchr /* See mm/README.txt */ #undef strchr /* See mm/README.txt */
FAR char *strchr(FAR const char *s, int c) FAR char *strchr(FAR const char *s, int c)
{ {
if (s) for (; ; s++)
{ {
for (; ; s++) if (*s == c)
{ {
if (*s == c) return (FAR char *)s;
{ }
return (FAR char *)s;
}
if (!*s) if (*s == 0)
{ {
break; break;
}
} }
} }

View file

@ -35,14 +35,11 @@
#undef strdup /* See mm/README.txt */ #undef strdup /* See mm/README.txt */
FAR char *strdup(FAR const char *s) FAR char *strdup(FAR const char *s)
{ {
FAR char *news = NULL; FAR char *news = (FAR char *)lib_malloc(strlen(s) + 1);
if (s)
if (news)
{ {
news = (FAR char *)lib_malloc(strlen(s) + 1); strcpy(news, s);
if (news)
{
strcpy(news, s);
}
} }
return news; return news;

View file

@ -53,24 +53,22 @@
FAR char *strndup(FAR const char *s, size_t size) FAR char *strndup(FAR const char *s, size_t size)
{ {
FAR char *news = NULL; FAR char *news = NULL;
if (s)
/* Get the size of the new string (limited to size) */
size_t allocsize = strnlen(s, size);
/* Allocate the new string, adding 1 for the NUL terminator */
news = (FAR char *)lib_malloc(allocsize + 1);
if (news)
{ {
/* Get the size of the new string (limited to size) */ /* Copy the string into the allocated memory and add a NUL
* terminator in any case.
*/
size_t allocsize = strnlen(s, size); memcpy(news, s, allocsize);
news[allocsize] = '\0';
/* Allocate the new string, adding 1 for the NUL terminator */
news = (FAR char *)lib_malloc(allocsize + 1);
if (news)
{
/* Copy the string into the allocated memory and add a NUL
* terminator in any case.
*/
memcpy(news, s, allocsize);
news[allocsize] = '\0';
}
} }
return news; return news;

View file

@ -33,15 +33,6 @@
#undef strpbrk /* See mm/README.txt */ #undef strpbrk /* See mm/README.txt */
FAR char *strpbrk(FAR const char *str, FAR const char *charset) FAR char *strpbrk(FAR const char *str, FAR const char *charset)
{ {
/* Sanity checking */
#ifdef CONFIG_DEBUG_FEATURES
if (!str || !charset)
{
return NULL;
}
#endif
/* Check each character in the string */ /* Check each character in the string */
while (*str) while (*str)

View file

@ -37,15 +37,13 @@
#undef strrchr /* See mm/README.txt */ #undef strrchr /* See mm/README.txt */
FAR char *strrchr(FAR const char *s, int c) FAR char *strrchr(FAR const char *s, int c)
{ {
if (s) FAR const char *p = &s[strlen(s)];
for (; p >= s; p--)
{ {
const char *p = &s[strlen(s)]; if (*p == c)
for (; p >= s; p--)
{ {
if (*p == c) return (FAR char *)p;
{
return (FAR char *)p;
}
} }
} }

View file

@ -70,10 +70,13 @@ static const char * const g_mon_name[12] =
FAR char *asctime_r(FAR const struct tm *tp, FAR char *buf) FAR char *asctime_r(FAR const struct tm *tp, FAR char *buf)
{ {
snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", char tmp[128];
snprintf(tmp, sizeof(tmp), "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n",
g_wday_name[tp->tm_wday], g_mon_name[tp->tm_mon], g_wday_name[tp->tm_wday], g_mon_name[tp->tm_mon],
tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec,
1900 + tp->tm_year); 1900 + tp->tm_year);
strlcpy(buf, tmp, 26);
return buf; return buf;
} }

View file

@ -389,7 +389,7 @@ static int local_getsockname(FAR struct socket *psock,
/* Copy the path into the user address structure */ /* Copy the path into the user address structure */
strncpy(unaddr->sun_path, conn->lc_path, namelen); strlcpy(unaddr->sun_path, conn->lc_path, namelen);
unaddr->sun_path[pathlen - 1] = '\0'; unaddr->sun_path[pathlen - 1] = '\0';
*addrlen = sizeof(sa_family_t) + namelen; *addrlen = sizeof(sa_family_t) + namelen;

View file

@ -200,7 +200,7 @@ FAR void *insmod(FAR const char *filename, FAR const char *modname)
/* Save the module name in the registry entry */ /* Save the module name in the registry entry */
strncpy(modp->modname, modname, MODLIB_NAMEMAX); strlcpy(modp->modname, modname, MODLIB_NAMEMAX);
/* Load the program binary */ /* Load the program binary */

View file

@ -205,11 +205,11 @@ static int work_thread_create(FAR const char *name, int priority,
FAR struct kwork_wqueue_s *wqueue) FAR struct kwork_wqueue_s *wqueue)
{ {
FAR char *argv[2]; FAR char *argv[2];
char args[16]; char args[32];
int wndx; int wndx;
int pid; int pid;
snprintf(args, 16, "0x%" PRIxPTR, (uintptr_t)wqueue); snprintf(args, sizeof(args), "0x%" PRIxPTR, (uintptr_t)wqueue);
argv[0] = args; argv[0] = args;
argv[1] = NULL; argv[1] = NULL;