Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: fix some errors found by cppcheck v1.67 #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

makomi
Copy link

@makomi makomi commented Jun 24, 2018

I used cppcheck v1.67 to fix some errors.

Issues fixed

src/utils.c:257: error (resourceLeak): Resource leak: rf
src/utils/common.c:599: error (memleak): Memory leak: bssid_copy
src/wps/wps_registrar.c:2498: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
src/wps/wps_registrar.c:2589: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
src/wps/wps_registrar.c:2596: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
src/wps/wps_registrar.c:2663: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
src/wps/wps_registrar.c:2670: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
src/wps/wps_registrar.c:2750: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
src/wps/wps_registrar.c:2757: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.

Remaining issues found

$ cppcheck --template "{file}:{line}: {severity} ({id}): {message}" \
  --quiet --force --enable=warning ./src/                           \
  | cut -d: -f3 | sort | uniq -c | sort -n
      1  warning (nullPointer)
      2  error (arrayIndexOutOfBounds)
      2  error (va_list_usedBeforeStarted)
      2  warning (invalidPrintfArgType_sint)
      2  warning (invalidscanf)

Going further

You might also want to enable style checks by using --enable=style instead of --enable=warning.

Furthermore, I think it would make sense to create a tools folder and add a BASH script like cppcheck.sh to it, in order to make finding these kind of defects easier. If this is something you want, I can create a respective pull request.

makomi added 3 commits June 24, 2018 15:05
Cppcheck 1.67

$ cppcheck --template "{file}:{line}: {severity} ({id}): {message}" \
  --quiet --force ./src/

> src/utils.c:257: error (resourceLeak): Resource leak: rf

File pointer 'rf' was overwritten by the following 'fopen' without
having been closed.

I understand that no resources were leaked in this case. In order to
avoid future resource leaks I decided to close it anyway.
Cppcheck 1.67

$ cppcheck --template "{file}:{line}: {severity} ({id}): {message}" \
  --quiet --force ./src/

> utils/common.c:599: error (memleak): Memory leak: bssid_copy

'bssid_copy' points to a region of memory that is allocated in this
function but never freed afterwards. 'bssid_copy' is stored on the
stack and "lost" once the function exits.
Cppcheck 1.67

$ cppcheck --template "{file}:{line}: {severity} ({id}): {message}" \
  --quiet --force ./src/

> wps/wps_registrar.c:2498: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
> wps/wps_registrar.c:2589: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
> wps/wps_registrar.c:2596: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
> wps/wps_registrar.c:2663: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
> wps/wps_registrar.c:2670: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
> wps/wps_registrar.c:2750: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.
> wps/wps_registrar.c:2757: error (invalidFunctionArgBool): Invalid memcmp() argument nr 3. A non-boolean value is required.

The data type of the third argument 'os_memcmp' expects is 'size_t'.
In these cases it was given a boolean expression that always evaluated
to 'true' and was hence interpreted by 'os_memcmp' as an integer '1'.
Therefore, instead of all nonce's bytes only one was actually compared.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant