Skip to content

Commit bf8c4ae

Browse files
committed
fix: stricter native implementation and type fix from PR feedback
1 parent 657c3b7 commit bf8c4ae

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

packages/machine-id/binding.cc

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace
1818

1919
#ifdef __APPLE__
2020
// Get macOS machine ID using IOKit framework directly
21-
std::string getMachineId()
21+
std::string getMachineId() noexcept
2222
{
2323
std::string uuid;
2424
io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMainPortDefault, "IOService:/");
@@ -34,10 +34,14 @@ namespace
3434
if (uuidProperty)
3535
{
3636
char buffer[128];
37-
if (CFStringGetCString((CFStringRef)uuidProperty, buffer, sizeof(buffer), kCFStringEncodingUTF8))
37+
if (!CFStringGetCString((CFStringRef)uuidProperty, buffer, sizeof(buffer), kCFStringEncodingUTF8))
3838
{
39-
uuid = buffer;
39+
CFRelease(uuidProperty);
40+
IOObjectRelease(ioRegistryRoot);
41+
return "";
4042
}
43+
44+
uuid = buffer;
4145
CFRelease(uuidProperty);
4246
}
4347

@@ -77,10 +81,12 @@ namespace
7781
if (file.is_open())
7882
{
7983
std::string line;
80-
if (std::getline(file, line))
84+
85+
if (!file.fail() && std::getline(file, line))
8186
{
8287
content = line;
8388
}
89+
8490
file.close();
8591
}
8692
return content;
@@ -94,21 +100,21 @@ namespace
94100
// Get Linux machine ID by reading from system files
95101
std::string getMachineId()
96102
{
97-
std::string id = readFile(DBUS_PATH);
103+
std::string uuid = readFile(DBUS_PATH);
98104

99105
// Try fallback path if the first path fails
100106
if (id.empty())
101107
{
102-
id = readFile(DBUS_PATH_ETC);
108+
uuid = readFile(DBUS_PATH_ETC);
103109
}
104110

105-
return trim(id);
111+
return trim(uuid);
106112
}
107113
#elif defined(_WIN32)
108114
// Get Windows machine ID from registry
109115
std::string getMachineId()
110116
{
111-
std::string machineGuid;
117+
std::string uuid;
112118
HKEY hKey;
113119
LONG result = RegOpenKeyExA(
114120
HKEY_LOCAL_MACHINE,
@@ -122,7 +128,7 @@ namespace
122128
return "";
123129
}
124130

125-
char value[128];
131+
char value[128] = {0};
126132
DWORD valueSize = sizeof(value);
127133
DWORD valueType;
128134

@@ -134,14 +140,19 @@ namespace
134140
reinterpret_cast<LPBYTE>(value),
135141
&valueSize);
136142

137-
RegCloseKey(hKey);
138-
139-
if (result == ERROR_SUCCESS && valueType == REG_SZ)
143+
if (result == ERROR_SUCCESS && valueType == REG_SZ && valueSize > 0)
144+
{
145+
// Create string with explicit length based on returned valueSize
146+
uuid = std::string(value, valueSize - (value[valueSize - 1] == '\0' ? 1 : 0));
147+
}
148+
else
140149
{
141-
machineGuid = value;
150+
RegCloseKey(hKey);
151+
return "";
142152
}
143153

144-
return machineGuid;
154+
RegCloseKey(hKey);
155+
return uuid;
145156
}
146157
#endif
147158

packages/machine-id/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function getMachineIdFromBinding(): string | undefined {
2424
export function getMachineId({ raw = false }: GetMachineIdOptions = {}):
2525
| string
2626
| undefined {
27-
const machineId: string | undefined = getMachineIdFromBinding();
27+
const machineId = getMachineIdFromBinding();
2828

2929
if (!machineId || raw === true) {
3030
return machineId;

0 commit comments

Comments
 (0)