Merge pull request #91 from HuskyDG/main

fix zygisk companion crash when fopen fails
This commit is contained in:
Marcos 2023-11-26 21:36:05 +01:00 committed by GitHub
commit 8eaa0f2490
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
#include <android/log.h> #include <android/log.h>
#include <sys/system_properties.h> #include <sys/system_properties.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include "zygisk.hpp" #include "zygisk.hpp"
#include "shadowhook.h" #include "shadowhook.h"
@ -214,35 +215,43 @@ private:
static void companion(int fd) { static void companion(int fd) {
FILE *dex = fopen(DEX_FILE_PATH, "rb"); FILE *dex = fopen(DEX_FILE_PATH, "rb");
long dexSize = 0;
char *dexBuffer = nullptr;
long jsonSize = 0;
char *jsonBuffer = nullptr;
fseek(dex, 0, SEEK_END); if (dex) {
long dexSize = ftell(dex); fseek(dex, 0, SEEK_END);
fseek(dex, 0, SEEK_SET); dexSize = ftell(dex);
fseek(dex, 0, SEEK_SET);
char dexBuffer[dexSize]; dexBuffer = (char*)calloc(1, dexSize);
fread(dexBuffer, 1, dexSize, dex); fread(dexBuffer, 1, dexSize, dex);
fclose(dex); fclose(dex);
}
FILE *json = fopen(JSON_FILE_PATH, "r"); FILE *json = fopen(JSON_FILE_PATH, "r");
fseek(json, 0, SEEK_END); if (json) {
long jsonSize = ftell(json); fseek(json, 0, SEEK_END);
fseek(json, 0, SEEK_SET); jsonSize = ftell(json);
fseek(json, 0, SEEK_SET);
char jsonBuffer[jsonSize]; jsonBuffer = (char*)calloc(1, jsonSize);
fread(jsonBuffer, 1, jsonSize, json); fread(jsonBuffer, 1, jsonSize, json);
fclose(json); fclose(json);
}
dexBuffer[dexSize] = 0;
jsonBuffer[jsonSize] = 0;
write(fd, &dexSize, sizeof(long)); write(fd, &dexSize, sizeof(long));
write(fd, dexBuffer, dexSize); write(fd, dexBuffer, dexSize);
write(fd, &jsonSize, sizeof(long)); write(fd, &jsonSize, sizeof(long));
write(fd, jsonBuffer, jsonSize); write(fd, jsonBuffer, jsonSize);
free(dexBuffer);
free(jsonBuffer);
} }