Use bool + size_t for os_version_str, return true on success

Abort the test if the function fails, as the version output will be undefined in that case.
This commit is contained in:
Robert Müller 2023-03-16 20:02:59 +01:00
parent 40cee85558
commit f88622085c
4 changed files with 18 additions and 22 deletions

View file

@ -4186,7 +4186,7 @@ int secure_rand_below(int below)
} }
} }
int os_version_str(char *version, int length) bool os_version_str(char *version, size_t length)
{ {
#if defined(CONF_FAMILY_WINDOWS) #if defined(CONF_FAMILY_WINDOWS)
const WCHAR *module_path = L"kernel32.dll"; const WCHAR *module_path = L"kernel32.dll";
@ -4194,20 +4194,20 @@ int os_version_str(char *version, int length)
DWORD size = GetFileVersionInfoSizeW(module_path, &handle); DWORD size = GetFileVersionInfoSizeW(module_path, &handle);
if(!size) if(!size)
{ {
return 1; return false;
} }
void *data = malloc(size); void *data = malloc(size);
if(!GetFileVersionInfoW(module_path, handle, size, data)) if(!GetFileVersionInfoW(module_path, handle, size, data))
{ {
free(data); free(data);
return 1; return false;
} }
VS_FIXEDFILEINFO *fileinfo; VS_FIXEDFILEINFO *fileinfo;
UINT unused; UINT unused;
if(!VerQueryValueW(data, L"\\", (void **)&fileinfo, &unused)) if(!VerQueryValueW(data, L"\\", (void **)&fileinfo, &unused))
{ {
free(data); free(data);
return 1; return false;
} }
str_format(version, length, "Windows %hu.%hu.%hu.%hu", str_format(version, length, "Windows %hu.%hu.%hu.%hu",
HIWORD(fileinfo->dwProductVersionMS), HIWORD(fileinfo->dwProductVersionMS),
@ -4215,12 +4215,12 @@ int os_version_str(char *version, int length)
HIWORD(fileinfo->dwProductVersionLS), HIWORD(fileinfo->dwProductVersionLS),
LOWORD(fileinfo->dwProductVersionLS)); LOWORD(fileinfo->dwProductVersionLS));
free(data); free(data);
return 0; return true;
#else #else
struct utsname u; struct utsname u;
if(uname(&u)) if(uname(&u))
{ {
return 1; return false;
} }
char extra[128]; char extra[128];
extra[0] = 0; extra[0] = 0;
@ -4261,7 +4261,7 @@ int os_version_str(char *version, int length)
} while(false); } while(false);
str_format(version, length, "%s %s (%s, %s)%s", u.sysname, u.release, u.machine, u.version, extra); str_format(version, length, "%s %s (%s, %s)%s", u.sysname, u.release, u.machine, u.version, extra);
return 0; return true;
#endif #endif
} }

View file

@ -2552,19 +2552,15 @@ int secure_rand();
*/ */
int secure_rand_below(int below); int secure_rand_below(int below);
/* /**
Function: os_version_str * Returns a human-readable version string of the operating system.
Returns a human-readable version string of the operating system *
* @param version Buffer to use for the output.
Parameters: * @param length Length of the output buffer.
version - Buffer to use for the output. *
length - Length of the output buffer. * @return true on success, false on failure.
*/
Returns: bool os_version_str(char *version, size_t length);
0 - Success in getting the version.
1 - Failure in getting the version.
*/
int os_version_str(char *version, int length);
#if defined(CONF_EXCEPTION_HANDLING) #if defined(CONF_EXCEPTION_HANDLING)
void init_exception_handler(); void init_exception_handler();

View file

@ -73,7 +73,7 @@ public:
#endif #endif
char aVersionStr[128]; char aVersionStr[128];
if(!os_version_str(aVersionStr, sizeof(aVersionStr))) if(os_version_str(aVersionStr, sizeof(aVersionStr)))
{ {
dbg_msg("engine", "operating system version: %s", aVersionStr); dbg_msg("engine", "operating system version: %s", aVersionStr);
} }

View file

@ -6,6 +6,6 @@
TEST(Os, VersionStr) TEST(Os, VersionStr)
{ {
char aVersion[128]; char aVersion[128];
EXPECT_FALSE(os_version_str(aVersion, sizeof(aVersion))); ASSERT_TRUE(os_version_str(aVersion, sizeof(aVersion)));
EXPECT_STRNE(aVersion, ""); EXPECT_STRNE(aVersion, "");
} }