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

Fix compile-time warnings and other nits #30

Merged
merged 1 commit into from
Jun 9, 2024
Merged

Fix compile-time warnings and other nits #30

merged 1 commit into from
Jun 9, 2024

Conversation

jrabinow
Copy link
Contributor

Fix compile-time warnings and other nits

  • prevent incorrect typecasting (comparison between signed and unsigned)
  • prevent buffer overflows by replacing sprintf with snprintf
  • remove unused variables
  • newline added in final output
  • add *.swp to gitignore
  • whitespace

@narugit
Copy link
Owner

narugit commented Jun 6, 2024

Hi @jrabinow
Thank you for creating PR!
I'll check.

@narugit
Copy link
Owner

narugit commented Jun 6, 2024

Before

$ sudo make clean
$ sudo make 2>&1 | grep "warning"
clang: warning: -framework IOKit: 'linker' input unused [-Wunused-command-line-argument]
smctemp.cc:61:1: warning: 'OSSpinLock' is deprecated: first deprecated in macOS 10.12 - Use os_unfair_lock() from <os/lock.h> instead [-Wdeprecated-declarations]
smctemp.cc:153:3: warning: 'IOMasterPort' is deprecated: first deprecated in macOS 12.0 [-Wdeprecated-declarations]
smctemp.cc:214:3: warning: 'OSSpinLockLock' is deprecated: first deprecated in macOS 10.12 - Use os_unfair_lock_lock() from <os/lock.h> instead [-Wdeprecated-declarations]
smctemp.cc:242:3: warning: 'OSSpinLockUnlock' is deprecated: first deprecated in macOS 10.12 - Use os_unfair_lock_unlock() from <os/lock.h> instead [-Wdeprecated-declarations]
smctemp.cc:326:3: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
smctemp.cc:326:20: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
6 warnings generated.
clang: warning: -framework IOKit: 'linker' input unused [-Wunused-command-line-argument]
smctemp_string.cc:20:3: warning: 'sprintf' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]
1 warning generated.
main.cc:26:26: warning: unused variable 'key' [-Wunused-variable]
main.cc:27:26: warning: unused variable 'val' [-Wunused-variable]
2 warnings generated.

After

$ sudo make clean
$ sudo make 2>&1 | grep "warning"
clang: warning: -framework IOKit: 'linker' input unused [-Wunused-command-line-argument]
smctemp.cc:61:1: warning: 'OSSpinLock' is deprecated: first deprecated in macOS 10.12 - Use os_unfair_lock() from <os/lock.h> instead [-Wdeprecated-declarations]
smctemp.cc:153:3: warning: 'IOMasterPort' is deprecated: first deprecated in macOS 12.0 [-Wdeprecated-declarations]
smctemp.cc:214:3: warning: 'OSSpinLockLock' is deprecated: first deprecated in macOS 10.12 - Use os_unfair_lock_lock() from <os/lock.h> instead [-Wdeprecated-declarations]
smctemp.cc:242:3: warning: 'OSSpinLockUnlock' is deprecated: first deprecated in macOS 10.12 - Use os_unfair_lock_unlock() from <os/lock.h> instead [-Wdeprecated-declarations]
smctemp.cc:326:24: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
5 warnings generated.
clang: warning: -framework IOKit: 'linker' input unused [-Wunused-command-line-argument]

Copy link
Owner

@narugit narugit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines -26 to -27
smctemp::UInt32Char_t key = { 0 };
smctemp::SmcVal_t val;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note:

main.cc:26:26: warning: unused variable 'key' [-Wunused-variable]
main.cc:27:26: warning: unused variable 'val' [-Wunused-variable]

memset(&inputStructure, 0, sizeof(SmcKeyData_t));
memset(&outputStructure, 0, sizeof(SmcKeyData_t));
memset(&val, 0, sizeof(SmcVal_t));

inputStructure.key = string_util::strtoul(key, 4, 16);
sprintf(val.key, key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note:

smctemp.cc:326:3: warning: 'sprintf' is deprecated: 
This function is provided for compatibility reasons only.  
Due to security concerns inherent in the design of sprintf(3), 
it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]

smctemp.cc Outdated
memset(&inputStructure, 0, sizeof(SmcKeyData_t));
memset(&outputStructure, 0, sizeof(SmcKeyData_t));
memset(&val, 0, sizeof(SmcVal_t));

inputStructure.key = string_util::strtoul(key, 4, 16);
sprintf(val.key, key);

snprintf(val.key, 5, key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
snprintf(val.key, 5, key);
snprintf(val.key, sizeof(val.key), key);

Could you use sizeof in second argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

- prevent incorrect typecasting (comparison between signed and unsigned)
- prevent buffer overflows by replacing sprintf with snprintf
- remove unused variables
- newline added in final output
- add *.swp to gitignore
- whitespace
Copy link
Owner

@narugit narugit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! LGTM

@narugit narugit merged commit 3f0f88a into narugit:main Jun 9, 2024
2 checks passed
@jrabinow jrabinow deleted the fix_warnings branch June 9, 2024 02:32
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.

2 participants