From a61edd480d31a5adfe20d254fae54dd34ce8d538 Mon Sep 17 00:00:00 2001 From: Maksim Masalski Date: Thu, 3 Jun 2021 14:46:09 +0800 Subject: [PATCH] lib: add default labels and comments to switch statements According to the Zephyr Coding Guideline all switch statements shall be well-formed. Added a default labels to switch-clauses without them. Added comments to the empty default cases. Found as a coding guideline violation (MISRA R16.1) by static coding scanning tool. Signed-off-by: Maksim Masalski --- lib/libc/minimal/source/stdlib/atoi.c | 6 ++++++ lib/os/cbprintf_complete.c | 14 ++++++++++++++ lib/os/fdtable.c | 6 ++++++ 3 files changed, 26 insertions(+) diff --git a/lib/libc/minimal/source/stdlib/atoi.c b/lib/libc/minimal/source/stdlib/atoi.c index 196a2e6736cb1e..909580e3b703e9 100644 --- a/lib/libc/minimal/source/stdlib/atoi.c +++ b/lib/libc/minimal/source/stdlib/atoi.c @@ -43,6 +43,12 @@ int atoi(const char *s) break; /* artifact to quiet coverity warning */ case '+': s++; + default: + /* Add an empty default with break, this is a defensive programming. + * Static analysis tool won't raise a violation if default is empty, + * but has that comment. + */ + break; } /* Compute n as a negative number to avoid overflow on INT_MIN */ while (isdigit(*s)) { diff --git a/lib/os/cbprintf_complete.c b/lib/os/cbprintf_complete.c index 528278b3ecc507..d45c12c3787ae4 100644 --- a/lib/os/cbprintf_complete.c +++ b/lib/os/cbprintf_complete.c @@ -555,6 +555,10 @@ static inline const char *extract_specifier(struct conversion *conv, unsupported = sizeof(ptrdiff_t) > 4; break; default: + /* Add an empty default with break, this is a defensive + * programming. Static analysis tool won't raise a violation + * if default is empty, but has that comment. + */ break; } } else { @@ -1303,6 +1307,10 @@ static inline void store_count(const struct conversion *conv, *(ptrdiff_t *)dp = (ptrdiff_t)count; break; default: + /* Add an empty default with break, this is a defensive programming. + * Static analysis tool won't raise a violation if default is empty, + * but has that comment. + */ break; } } @@ -1665,6 +1673,12 @@ int cbvprintf(cbprintf_cb out, void *ctx, const char *fp, va_list ap) &sign, buf, &bpe); } break; + default: + /* Add an empty default with break, this is a defensive + * programming. Static analysis tool won't raise a violation + * if default is empty, but has that comment. + */ + break; } /* If we don't have a converted value to emit, move diff --git a/lib/os/fdtable.c b/lib/os/fdtable.c index e08b541e732fca..3b737e77b32b5c 100644 --- a/lib/os/fdtable.c +++ b/lib/os/fdtable.c @@ -309,6 +309,12 @@ int fcntl(int fd, int cmd, ...) /* Not implemented so far. */ errno = EINVAL; return -1; + default: + /* Add an empty default with break, this is a defensive programming. + * Static analysis tool won't raise a violation if default is empty, + * but has that comment. + */ + break; } /* The rest of commands are per-fd, handled by ioctl vmethod. */