mirror of
https://github.com/yuzu-emu/discord-rpc.git
synced 2024-12-23 05:35:28 +00:00
Fix buffer overflows in RegisterCommand on mac (#99)
This commit is contained in:
parent
839ba32671
commit
2c609b1d5f
|
@ -5,45 +5,28 @@
|
||||||
|
|
||||||
#include "discord_register.h"
|
#include "discord_register.h"
|
||||||
|
|
||||||
static bool Mkdir(const char* path)
|
|
||||||
{
|
|
||||||
int result = mkdir(path, 0755);
|
|
||||||
if (result == 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (errno == EEXIST) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void RegisterCommand(const char* applicationId, const char* command)
|
static void RegisterCommand(const char* applicationId, const char* command)
|
||||||
{
|
{
|
||||||
// There does not appear to be a way to register arbitrary commands on OSX, so instead we'll save the command
|
// There does not appear to be a way to register arbitrary commands on OSX, so instead we'll save the command
|
||||||
// to a file in the Discord config path, and when it is needed, Discord can try to load the file there, open
|
// to a file in the Discord config path, and when it is needed, Discord can try to load the file there, open
|
||||||
// the command therein (will pass to js's window.open, so requires a url-like thing)
|
// the command therein (will pass to js's window.open, so requires a url-like thing)
|
||||||
|
|
||||||
const char* home = getenv("HOME");
|
// Note: will not work for sandboxed apps
|
||||||
|
NSString *home = NSHomeDirectory();
|
||||||
if (!home) {
|
if (!home) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char path[2048];
|
NSString *path = [[[[[[home stringByAppendingPathComponent:@"Library"]
|
||||||
sprintf(path, "%s/Library/Application Support/discord", home);
|
stringByAppendingPathComponent:@"Application Support"]
|
||||||
Mkdir(path);
|
stringByAppendingPathComponent:@"discord"]
|
||||||
strcat(path, "/games");
|
stringByAppendingPathComponent:@"games"]
|
||||||
Mkdir(path);
|
stringByAppendingPathComponent:[NSString stringWithUTF8String:applicationId]]
|
||||||
strcat(path, "/");
|
stringByAppendingPathExtension:@"json"];
|
||||||
strcat(path, applicationId);
|
[[NSFileManager defaultManager] createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
|
||||||
strcat(path, ".json");
|
|
||||||
|
|
||||||
FILE* f = fopen(path, "w");
|
NSString *jsonBuffer = [NSString stringWithFormat:@"{\"command\": \"%s\"}", command];
|
||||||
if (f) {
|
[jsonBuffer writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil];
|
||||||
char jsonBuffer[2048];
|
|
||||||
int len = snprintf(jsonBuffer, sizeof(jsonBuffer), "{\"command\": \"%s\"}", command);
|
|
||||||
fwrite(jsonBuffer, (size_t)len, 1, f);
|
|
||||||
fclose(f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void RegisterURL(const char* applicationId)
|
static void RegisterURL(const char* applicationId)
|
||||||
|
@ -83,15 +66,15 @@ void Discord_Register(const char* applicationId, const char* command)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// raii lite
|
// raii lite
|
||||||
void* pool = [[NSAutoreleasePool alloc] init];
|
@autoreleasepool {
|
||||||
RegisterURL(applicationId);
|
RegisterURL(applicationId);
|
||||||
[(id)pool drain];
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Discord_RegisterSteamGame(const char* applicationId, const char* steamId)
|
void Discord_RegisterSteamGame(const char* applicationId, const char* steamId)
|
||||||
{
|
{
|
||||||
char command[256];
|
char command[256];
|
||||||
sprintf(command, "steam://rungameid/%s", steamId);
|
snprintf(command, 256, "steam://rungameid/%s", steamId);
|
||||||
Discord_Register(applicationId, command);
|
Discord_Register(applicationId, command);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue