Skip to content

Commit d408303

Browse files
committed
Prevent memory leak while loading smbios data
1 parent 75ea749 commit d408303

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ int main(int argc, char *argv[])
2626
// Get SMBIOS information using a custom function.
2727
// An example in C for Windows and Linux can be found
2828
// in 'smbios_decode.c'.
29-
const uint8_t *data = NULL;
29+
uint8_t *data = NULL;
3030
size_t size = 0;
31-
load_smbios_data(&data, &size);
31+
if (!get_dmi_data("/sys/firmware/dmi/tables", &data, &size))
32+
return 1;
3233

3334
// initialize the parser (the desired SMBIOS version is 3.0)
3435
struct ParserContext context;
3536
if (smbios_initialize(&context, data, size, SMBIOS_3_0) != SMBERR_OK)
37+
{
38+
free(data);
3639
return 1;
40+
}
3741

3842
// iterate over the SMBIOS entries
3943
const struct Entry *entry = NULL;
@@ -42,6 +46,8 @@ int main(int argc, char *argv[])
4246
// do something with the current entry
4347
}
4448

49+
free(data);
50+
4551
return 0;
4652
}
4753
```

smbios_decode.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ static bool get_dmi_data( uint8_t **buffer, size_t *size )
3737
if (*buffer == NULL)
3838
return false;
3939
// retrieve the SMBIOS table
40-
return (*size == GetSystemFirmwareTable(signature, 0, *buffer, *size))
40+
if (*size != GetSystemFirmwareTable(signature, 0, *buffer, *size))
41+
{
42+
free(*buffer);
43+
return false;
44+
}
45+
return true;
4146
}
4247

4348
#else
@@ -60,15 +65,21 @@ static bool get_dmi_data( const char *path, uint8_t **buffer, size_t *size )
6065
// read SMBIOS structures
6166
input = fopen(fileName, "rb");
6267
if (input == NULL)
68+
{
69+
free(*buffer);
6370
return false;
71+
}
6472
fread((char*) *buffer + 32, (size_t) info.st_size, 1, input);
6573
fclose(input);
6674

6775
// read SMBIOS entry point
6876
snprintf(fileName, sizeof(fileName), "%s/smbios_entry_point", path);
6977
input = fopen(fileName, "rb");
7078
if (input == NULL)
79+
{
80+
free(*buffer);
7181
return false;
82+
}
7283
fread((char*) *buffer, 32, 1, input);
7384
fclose(input);
7485

@@ -481,15 +492,15 @@ int main(int argc, char ** argv)
481492

482493
if (!result)
483494
{
484-
fputs("Unable to open SMBIOS tables", stderr);
495+
fputs("Unable to open SMBIOS tables\n", stderr);
485496
return 1;
486497
}
487498

488499
struct ParserContext parser;
489500
if (smbios_initialize(&parser, buffer, size, SMBIOS_ANY) == SMBERR_OK)
490501
printSMBIOS(&parser, stdout);
491502
else
492-
fputs("Invalid SMBIOS data", stderr);
503+
fputs("Invalid SMBIOS data\n", stderr);
493504

494505
free(buffer);
495506
return 0;

0 commit comments

Comments
 (0)