Skip to content

Commit

Permalink
qa: adapt gcc warnings and fix type conversions
Browse files Browse the repository at this point in the history
fixes a bad type conversion between signed and unsigned int
which hid a wrong if clause expression in fread'ing
files from sysfs.

Even if `fread()` would have failed the `strcmp()` would
fail and prevent bad/unexpected things.

Worst that may happen is a false device detection that does
not recognize your yoga notebook.
  • Loading branch information
esno committed Jan 14, 2021
1 parent dc42e4f commit 38c2495
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
yoga-unlock:
${CC} -o ./yoga-bios-unlock ./src/yoga-bios-unlock.c -O2 -Wall
${CC} -o ./yoga-bios-unlock ./src/yoga-bios-unlock.c -O2 -Wall -Wextra -Wfloat-equal -Wshadow -Wstrict-prototypes -Wstrict-overflow=5 -Wcast-qual -Wconversion -Wunreachable-code -fanalyzer

clean:
rm ./yoga-bios-unlock
12 changes: 6 additions & 6 deletions src/yoga-bios-unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ struct dmi_strings {

int check_dmi(const char *file, dmi_strings_t *dmi);
int is_yoga(void);
int read_sysfs(const char *file, char *buffer, int n);
int read_sysfs(const char *file, char *buffer, size_t n);

int check_dmi(const char *file, dmi_strings_t *dmi) {
int l = 128;
size_t l = 128;
char buffer[l];
dmi_strings_t *ptr = dmi;
int rc = -2;
Expand Down Expand Up @@ -86,19 +86,19 @@ int is_yoga(void) {
return rc;
}

int read_sysfs(const char *file, char *buffer, int n) {
int l = strlen(__DMI_PATH) + strlen(file) + 2;
int read_sysfs(const char *file, char *buffer, size_t n) {
size_t l = strlen(__DMI_PATH) + strlen(file) + 2;
char filename[l];
FILE *fd;
int c;
size_t c;

memset(filename, 0, l);
snprintf(filename, l, "%s/%s", __DMI_PATH, file);
fd = fopen(filename, "r");
if (fd == NULL)
return -1;

if ((c = fread(buffer, 1, n, fd)) < 0) {
if ((c = fread(buffer, 1, n, fd)) != n) {
if (feof(fd) == 0) {
fclose(fd);
return -2;
Expand Down

0 comments on commit 38c2495

Please sign in to comment.