Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-2836

S3807: Cover the once promised list of functions

    XMLWordPrintable

    Details

      Description

      A version of RSPEC-3807 is cited below. The current, updated version of the rspec reflects the implementation of the rule, yet it would be nice to cover the functions that we once promised we cover.

      This ticket is to update the implementation to cover the functions from the list below that are still missing, and to merge the rspec below with the current version of RSPEC-3807.

      the version of the RSPEC

      The standard C library includes a number of functions for string and memory manipulation. They take pointers and lengths as parameters. Passing NULL for the pointers will at best do nothing and at worst crash the application.

      This rule raises an issue when NULL is passed as a pointer to any of the following functions:

      • void *memcpy(void *dest, const void *src, size_t n);
      • void *memmove(void *dest, const void *src, size_t n);
      • void *memccpy(void *dest, const void *src, int c, size_t n);
      • void *memset(void *s, int c, size_t n);
      • int memcmp(const void *s1, const void *s2, size_t n);
      • void *memchr(const void *s, int c, size_t n);
      • void *rawmemchr(const void *s, int c);
      • void *memrchr(const void *s, int c, size_t n);
      • char *strcpy(char *dest, const char *src);
      • char *strncpy(char *dest, const char *src, size_t n);
      • char *strcat(char *dest, const char *src);
      • char *strncat(char *dest, const char *src, size_t n);
      • int strcmp(const char *s1, const char *s2);
      • int strncmp(const char *s1, const char *s2, size_t n);
      • int strcoll(const char *s1, const char *s2);
      • size_t strxfrm(char *dest, const char *src, size_t n);
      • int strcoll_l(const char *s1, const char *s2, locale_t loc);
      • size_t strxfrm_l(char *restrict s1, const char *restrict s2, size_t n, locale_t loc);
      • char *strdup(const char *s);
      • char *strndup(const char *s, size_t n);
      • char *strchr(const char *s, int c);
      • char *strrchr(const char *s, int c);
      • char *strchrnul(const char *s, int c);
      • size_t strcspn(const char *s, const char *reject);
      • size_t strspn(const char *s, const char *accept);
      • char *strpbrk(const char *s, const char *accept);
      • char *strstr(const char *haystack, const char *needle);
      • char *strtok(char *s, const char *delim);
      • char *strtok_r(char *str, const char *delim, char **saveptr);
      • char *strcasestr(const char *haystack, const char *needle);
      • void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen);
      • void *mempcpy(void *dest, const void *src, size_t n);
      • size_t strlen(const char *s);
      • size_t strnlen(const char *s, size_t maxlen);
      • char *strerror_r(int errnum, char *buf, size_t buflen);
      • void bcopy(const void *src, void *dest, size_t n);
      • void bzero(void *s, size_t n);
      • int bcmp(const void *s1, const void *s2, size_t n);
      • char *index(const char *s, int c);
      • char *rindex(const char *s, int c);
      • int strcasecmp(const char *s1, const char *s2);
      • int strncasecmp(const char *s1, const char *s2, size_t n);
      • int strcasecmp_l(const char *s1, const char *s2, locale_t loc);
      • int strncasecmp_l(const char *s1, const char *s2, size_t n, locale_t loc);
      • char *strsep(char **stringp, const char *delim);
      • char *stpcpy(char *dest, const char *src);
      • char *stpncpy(char *dest, const char *src, size_t n);
      • int strverscmp(const char *s1, const char *s2);
      • char *strfry(char *string);
      • void *memfrob(void *s, size_t n);
      • char *basename(char *path);

      This rule raises an issue when 0 is passed as a length to any of the following functions, since the last two parameters might have been swapped by mistake:

      • void *memccpy(void *dest, const void *src, int c, size_t n);
      • void *memset(void *s, int c, size_t n);
      • void *memchr(const void *s, int c, size_t n);
      • void *memrchr(const void *s, int c, size_t n);
      • void bzero(void *s, size_t n);

      This rule also raises an issue when sizeof is passed as a second argument of memset, since it is another case where the last two parameters might have been swapped by mistake.

      Noncompliant Code Example

      memcpy(NULL, src, 10); // Noncompliant, null pointer
      memset(ptr, 0, 0); // Noncompliant, length is zero
      memset(dest, sizeof(src), src); // Noncompliant, "sizeof" should be the third argument
      

      Additional functions

      that are supposed to be covered by our imlementation, yet I failed to trigger the rule on them:

      • size_t confstr(int name, char* buf, size_t len);
      • size_t fread(void* restrict ptr, size_t size, size_t nmemb, FILE* restrict stream);
      • size_t fwrite(const void* restrict ptr, size_t size, size_t nmemb, FILE* restrict stream);
      • ssize_t getdelim(char** restrict linep, size_t* restrict linecapp, int delimiter, FILE* restrict stream);
      • size_t iconv(iconv_t cd, char** restrict src, size_t* restrict srcleft, char** restrict dst, size_t* restrict dstleft);
      • size_t mbrlen(const char* restrict s, size_t n, mbstate_t* restrict ps);
      • size_t mbrtowc(wchar_t* restrict pc, const char* restrict s, size_t n, mbstate_t* restrict ps);
      • size_t mbsrtowcs(wchar_t* restrict dst, const char** restrict src, size_t len, mbstate_t* restrict ps);
      • size_t mbstowcs(wchar_t* restrict wcstring, const char* restrict mbstring, size_t nwchars);
      • int mbtowc(wchar_t* restrict wcharp, const char* restrict mbchar, size_t nbytes);
      • size_t regerror(int errcode, const regex_t* restrict preg, char* restrict errbuf, size_t errbuf_size);
      • int regexec(const regex_t* restrict preg, const char* restrict string, size_t nmatch, regmatch_t pmatch[restrict], int eflags);
      • int setvbuf(FILE* restrict stream, char* restrict buf, int mode, size_t size);
      • int snprintf(char* restrict str, size_t size, const char* restrict format, ...);
      • ssize_t strfmon(char* restrict s, size_t maxsize, const char* restrict format, ...);
      • ssize_t strfmon_l(char* restrict s, size_t maxsize, locale_t loc, const char* restrict format, ...);
      • size_t strftime_l(char* restrict buf, size_t maxsize, const char* restrict format, const struct tm* restrict timeptr, locale_t loc);
      • int swprintf(wchar_t* restrict ws, size_t n, const wchar_t* restrict format, ...);
      • int vsnprintf(char* restrict str, size_t size, const char* restrict format, va_list ap);
      • int wcsncasecmp_l(const wchar_t* ws1, const wchar_t* ws2, size_t n, locale_t locale);
      • size_t wcsrtombs(char* dest, const wchar_t** src, size_t len, mbstate_t* ps);
      • size_t wcstombs(char* dest, const wchar_t* src, size_t n);
      • size_t wcsxfrm_l(wchar_t* restrict ws1, const wchar_t* restrict ws2, size_t n, locale_t locale);
      • wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n);

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              arseniy.zaostrovnykh Arseniy Zaostrovnykh
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: