6429: Minor refactoring of `os_version_str` r=edg-l a=Robyt3



## Checklist

- [X] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test (especially base/) or added coverage to integration test
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Robert Müller <robytemueller@gmail.com>
This commit is contained in:
bors[bot] 2023-05-14 09:24:54 +00:00 committed by GitHub
commit 648dc9e032
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 23 deletions

View file

@ -4146,7 +4146,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)
const WCHAR *module_path = L"kernel32.dll";
@ -4154,20 +4154,20 @@ int os_version_str(char *version, int length)
DWORD size = GetFileVersionInfoSizeW(module_path, &handle);
if(!size)
{
return 1;
return false;
}
void *data = malloc(size);
if(!GetFileVersionInfoW(module_path, handle, size, data))
{
free(data);
return 1;
return false;
}
VS_FIXEDFILEINFO *fileinfo;
UINT unused;
if(!VerQueryValueW(data, L"\\", (void **)&fileinfo, &unused))
{
free(data);
return 1;
return false;
}
str_format(version, length, "Windows %hu.%hu.%hu.%hu",
HIWORD(fileinfo->dwProductVersionMS),
@ -4175,12 +4175,12 @@ int os_version_str(char *version, int length)
HIWORD(fileinfo->dwProductVersionLS),
LOWORD(fileinfo->dwProductVersionLS));
free(data);
return 0;
return true;
#else
struct utsname u;
if(uname(&u))
{
return 1;
return false;
}
char extra[128];
extra[0] = 0;
@ -4221,7 +4221,7 @@ int os_version_str(char *version, int length)
} while(false);
str_format(version, length, "%s %s (%s, %s)%s", u.sysname, u.release, u.machine, u.version, extra);
return 0;
return true;
#endif
}

View file

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

View file

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

View file

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