User Activity

  • Posted a comment on discussion General Discussion on cppcheck

    Hello, following code: #include <stdio.h> #include <stdint.h> #include <stdlib.h> static void Check(unsigned c){if(!c){exit(1);}} static void CheckArr(uint8_t byCount, const uint8_t *pData) { uint8_t i,j; for(i = 0; i < byCount;i++) { for(j = i + 1; j < byCount;j++) { Check(pData[i] != pData[j]); } } } int main() { uint8_t x = 1; CheckArr(1,&x); return 0; } Causes warning: tmp.c:14:36: warning: The address of variable 'x' might be accessed at non-zero index. [objectIndex] Check(pData[i] != pData[j]);...

  • Posted a comment on discussion General Discussion on cppcheck

    Yes, it works now:-) Thank you for help.:-)

  • Posted a comment on discussion General Discussion on cppcheck

    Hello, have routine: static void PrintFailed(const char *pszText) { DWORD dwLastError = GetLastError(); char *pszErrorMessage = NULL; const DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK; FormatMessageA(dwFlags,NULL, dwLastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPSTR)&pszErrorMessage, 0, NULL); fprintf(stderr,"%s failed. Error: %lu (%s)\n", pszText,dwLastError,pszErrorMessage ? pszErrorMessage...

  • Posted a comment on discussion General Discussion on cppcheck

    Yes, it is dangerous to call GetValue(0), however this case will never happen. The function is static, so not callable from outside of this source and the only place of call is safe, because routine Test() always calls with n at least 1.

  • Posted a comment on discussion General Discussion on cppcheck

    Hello, have a code: #include <stdint.h> static uint8_t GetValue(uint8_t n) { uint8_t i,buf[10]; for(i = 0; i < n; i++) { buf[i] = i; } return buf[n / 2]; } uint8_t Test(uint8_t k) { return GetValue((k & 7) + 1); } cppcheck 2.14 says: tmp.c:10:12: warning: Uninitialized variable: buf [uninitvar] return buf[n / 2]; ^ tmp.c:6:18: note: Assuming condition is false for(i = 0; i < n; i++) ^ tmp.c:10:12: note: Uninitialized variable: buf return buf[n / 2]; ^ However n is always at least 1, so the used part...

  • Posted a comment on discussion General Discussion on cppcheck

    Have code: int K[2]; void test(void) { memcpy(&K[0],&K[1],sizeof(K[0])); } Cppcheck 2.14 generates warning: tmp.c:4:5: error: Overlapping read/write in memcpy() is undefined behavior [overlappingWriteFunction] memcpy(&K[0],&K[1],sizeof(K[0])); ^ However there is no overlap.

  • Posted a comment on discussion General Discussion on cppcheck

    Have code: void f() { unsigned x,y,s = 0; for(x = 0; x < 256;x++) { for(y = 0; y < 256; y++) { unsigned z = (y <= x) ? 255 : (x * 255) / y; s += z; } } printf("%u\n",s); } Both CPPCheck 2.13 and online demo report division by zero: error: Division by zero. [zerodiv] unsigned z = (y <= x) ? 255 : (x * 255) / y; Assignment 'y=0', assigned value is 0 for(y = 0; y < 256; y++) But the code will never divide by zero because of that condition (y <= x)

  • Posted a comment on discussion General Discussion on cppcheck

    Hello, I have following code: void g(const char *text); void f(const char *text) { unsigned offset = 0; if(!text) { text = "AA"; } else if(!strncmp(text,"abcd",4)) { offset = 4; } g(text + offset); } Both Cppcheck 2.11 and online demo claim, that [test.cpp:13]: (portability) Undefined behaviour, pointer arithmetic 'text+offset' is out of bounds. If I remove line text = "AA"; problem disappears. It looks like cppcheck assumes I use offset 4 for text = "AA". Best Regards Martin

View All

Personal Data

Username:
mpoupe
Joined:
2023-07-07 13:48:29.998000

Projects

  • No projects to display.

Personal Tools

Auth0 Logo