Skip to content

Commit e89e326

Browse files
Fix hooks causing OOM
1 parent d1da800 commit e89e326

5 files changed

Lines changed: 33 additions & 30 deletions

File tree

extractor/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void index_file(char *path, char* filename, bool new) {
190190

191191
char currentByte = 0;
192192
int processedBits = 0;
193-
for (int i=(b64Pointer - header.icon); i < strlen(header.icon); i++) {
193+
for (size_t i=(b64Pointer - header.icon); i < strlen(header.icon); i++) {
194194
// Convert the base64 character to binary
195195
char value = 0;
196196
if (header.icon[i] >= 'A' && header.icon[i] <= 'Z') {
@@ -240,7 +240,7 @@ void index_file(char *path, char* filename, bool new) {
240240
// If the file is functional, run install hook
241241
char* escapedPath = malloc(strlen(full_path)*2 + 1);
242242
int escapedPathLength = 0;
243-
for (int i=0; i < strlen(full_path); i++) {
243+
for (size_t i=0; i < strlen(full_path); i++) {
244244
if (full_path[i] == '"') {
245245
escapedPath[escapedPathLength++] = '\\';
246246
}
@@ -269,9 +269,9 @@ void index_file(char *path, char* filename, bool new) {
269269
Log("Writing script to %s\n", sdrFilePath);
270270
FILE* scriptFile = fopen(full_path, "r");
271271
FILE* sdrFile = fopen(sdrFilePath, "w");
272-
char c;
273-
while ((c = getc(scriptFile)) != EOF) {
274-
putc(c, sdrFile);
272+
int c;
273+
while ((c = fgetc(scriptFile)) != EOF) {
274+
fputc(c, sdrFile);
275275
}
276276
Log("Done!");
277277
fclose(scriptFile);
@@ -333,7 +333,7 @@ void remove_file(const char* path, const char* filename, char* uuid) {
333333
Log("Script uses hooks!");
334334
char* escapedPath = malloc(strlen(sdrScriptPath)*2 + 1);
335335
int escapedPathLength = 0;
336-
for (int i=0; i < strlen(sdrScriptPath); i++) {
336+
for (size_t i=0; i < strlen(sdrScriptPath); i++) {
337337
if (sdrScriptPath[i] == '"') {
338338
escapedPath[escapedPathLength++] = '\\';
339339
}

launcher/main.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
pid_t app_pid = -1;
1616
bool shouldExit = false;
1717

18-
LIPCcode stub(LIPC *lipc, const char *property, void *value, void *data) {
18+
LIPCcode stub(LIPC *lipc, const char *property, void *value, void*) {
1919
syslog(LOG_INFO, "Stub called for \"%s\" with value \"%s\"", property,
2020
(char *)value);
2121
char *id = strtok((char *)value, ":");
22-
char *response = malloc(strlen(id) + 3 + 1);
22+
char *response = (char*) malloc(strlen(id) + 3 + 1);
2323
snprintf(response, strlen(id) + 3 + 1, "%s:0:", id);
2424
syslog(LOG_INFO, "Replying with %s", response);
25-
char *target = malloc(strlen(property) + 6 + 1);
25+
char *target = (char*) malloc(strlen(property) + 6 + 1);
2626
snprintf(target, strlen(property) + 6 + 1, "%sresult", property);
2727
syslog(LOG_INFO, "Replying with %s, %s", target, response);
2828
LipcSetStringProperty(lipc, "com.lab126.appmgrd", target, response);
@@ -64,9 +64,9 @@ char* getScriptCommand(char* scriptPath)
6464
readScriptHeader(file, &header);
6565
fclose(file);
6666

67-
char* escapedPath = malloc((strlen(scriptPath) * 2) + 1);
67+
char* escapedPath = (char*) malloc((strlen(scriptPath) * 2) + 1);
6868
int escapedPathLength = 0;
69-
for (int i=0; i < strlen(scriptPath); i++) {
69+
for (size_t i=0; i < strlen(scriptPath); i++) {
7070
if (scriptPath[i] == '"') {
7171
escapedPath[escapedPathLength++] = '\\';
7272
}
@@ -93,7 +93,7 @@ char* getScriptCommand(char* scriptPath)
9393
}
9494

9595
LIPCcode go_callback(LIPC* lipc, const char* property, void* value, void* data) {
96-
char* rawFilePath = strchr(value, ':') + 6 + strlen(SERVICE_NAME) + 1;
96+
char* rawFilePath = strchr((const char*)value, ':') + 6 + strlen(SERVICE_NAME) + 1;
9797
char* query = strchr(rawFilePath, '?');
9898
if (query != NULL) {
9999
query[0] = 0;

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
project('sh_integration', 'c', version : '1.0.0', default_options: ['cpp_std=c++17'])
1+
project('sh_integration', 'c', version : '1.0.0', default_options: ['warning_level=everything'])
22

33
# Fetch our dependencies
44
subproject('cjson', default_options: ['default_library=static']).get_variable('libcjson_dep')

utils/utils.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "utils.h"
2+
#include <stddef.h>
23
#include <stdlib.h>
34
#include <string.h>
45

56
char* urlDecode(char* raw)
67
{
7-
char* result = malloc(strlen(raw) + 1); // URLEncoded string will NEVER be longer decoded
8+
char* result = (char*) malloc(strlen(raw) + 1); // URLEncoded string will NEVER be longer decoded
89
int currentFilepathLen = 0;
910

1011
for (size_t i=0; i < strlen(raw); i++) {
@@ -19,6 +20,13 @@ char* urlDecode(char* raw)
1920
return result;
2021
}
2122

23+
char* buildCommand(const char* command, const char* sub)
24+
{
25+
char* builtCommand = (char*) malloc(strlen(command) + strlen(sub) + 1);
26+
sprintf(builtCommand, command, sub);
27+
return builtCommand;
28+
}
29+
2230
void recursiveDelete(char* path)
2331
{
2432
DIR* dir = opendir(path);
@@ -29,7 +37,7 @@ void recursiveDelete(char* path)
2937
{
3038
continue;
3139
}
32-
char* itemPath = malloc(strlen(path) + 1 + strlen(item->d_name) + 1);
40+
char* itemPath = (char*) malloc(strlen(path) + 1 + strlen(item->d_name) + 1);
3341
sprintf(itemPath, "%s/%s", path, item->d_name);
3442
if (item->d_type == DT_DIR)
3543
{
@@ -42,13 +50,6 @@ void recursiveDelete(char* path)
4250
closedir(dir);
4351
}
4452

45-
char* buildCommand(char* command, char* sub)
46-
{
47-
char* builtCommand = malloc(strlen(command) + strlen(sub) + 1);
48-
sprintf(builtCommand, command, sub);
49-
return builtCommand;
50-
}
51-
5253
void freeScriptHeader(struct ScriptHeader* header)
5354
{
5455
free(header->name);
@@ -58,7 +59,7 @@ void freeScriptHeader(struct ScriptHeader* header)
5859
header->name = NULL;
5960
header->author = NULL;
6061
header->icon = NULL;
61-
};
62+
}
6263

6364
void readScriptHeader(FILE* file, struct ScriptHeader* header)
6465
{
@@ -68,12 +69,12 @@ void readScriptHeader(FILE* file, struct ScriptHeader* header)
6869
header->useFBInk=true;
6970
header->useHooks=false;
7071

71-
int bufferSize = 1024;
72-
char* buffer = malloc(bufferSize);
72+
size_t bufferSize = 1024;
73+
char* buffer = (char*) malloc(bufferSize);
7374
fseek(file, 0, SEEK_SET);
7475
for (int i=0; i < 6; i++) {
7576
buffer[0] = '\0';
76-
int lineLength = 0;
77+
size_t lineLength = 0;
7778
int c;
7879
while ((c = fgetc(file)) != EOF)
7980
{
@@ -84,15 +85,15 @@ void readScriptHeader(FILE* file, struct ScriptHeader* header)
8485

8586
if (lineLength + 2 >= bufferSize)
8687
{
87-
buffer = realloc(buffer, bufferSize+=(lineLength + 2));
88+
buffer = (char*) realloc(buffer, bufferSize+=(lineLength + 2));
8889
if (buffer == NULL)
8990
{
9091
printf("FATAL - FAILED TO REALLOC BUFFER!!!\n");
9192
return; //@TODO: We don't really have a good way of dealing with this
9293
}
9394
}
9495

95-
buffer[lineLength++] = c;
96+
buffer[lineLength++] = (char) c;
9697
}
9798
buffer[lineLength] = '\0';
9899

@@ -124,4 +125,4 @@ void readScriptHeader(FILE* file, struct ScriptHeader* header)
124125
}
125126
}
126127
free(buffer);
127-
};
128+
}

utils/utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
#include <stdio.h>
55
#include <sys/types.h>
66
#include <dirent.h>
7+
#include <stdlib.h>
8+
#include <string.h>
79

810
char* urlDecode(char* raw);
911

1012
void recursiveDelete(char* path);
1113

12-
char* buildCommand(char* command, char* sub);
14+
char* buildCommand(const char* command, const char* sub);
1315

1416
struct ScriptHeader
1517
{

0 commit comments

Comments
 (0)