aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c9
-rw-r--r--reloc.c18
2 files changed, 17 insertions, 10 deletions
diff --git a/main.c b/main.c
index 3b3e467..b8e8a04 100644
--- a/main.c
+++ b/main.c
@@ -42,13 +42,14 @@ int main(int argc, char *argv[]) {
char *replacement = strdup(argv[2]);
char *input_file = strdup(argv[3]);
char *output_file = strdup(argv[4]);
- RelocData *info = reloc_read(input_file);
- if (!info) {
+ size_t records = 0;
+ size_t replacement_length = strlen(replacement);
+ RelocData *info = NULL;
+
+ if (!(info = reloc_read(input_file))) {
reloc_perror(input_file);
exit(reloc_error);
}
- size_t records = 0;
- size_t replacement_length = strlen(replacement);
for (size_t i = 0; i < info->size; i++) {
RelocMatch *match = NULL;
diff --git a/reloc.c b/reloc.c
index 7ce1911..c08f1de 100644
--- a/reloc.c
+++ b/reloc.c
@@ -2,18 +2,20 @@
RelocMatch *reloc_match(char *haystack, const char *needle) {
char *data = haystack;
- size_t needle_size = strlen(needle);
+ char *pattern = strdup(needle);
+ size_t pattern_size = strlen(pattern);
RelocMatch *match = NULL;
// Search the needle in the data
- if (!(memcmp(data, needle, needle_size))) {
- if (!(match = calloc(1, sizeof(RelocMatch)))) {
+ if (!(memcmp(data, pattern, pattern_size))) {
+ if (!(match = (RelocMatch *)calloc(1, sizeof(RelocMatch)))) {
reloc_error = RELOC_ENOMEM;
+ free(pattern);
return NULL;
}
size_t data_end = strlen(data);
match->begin = data;
- match->end = match->begin + needle_size;
+ match->end = match->begin + pattern_size;
match->length = match->end - match->begin;
match->post = match->end;
match->post_length = strlen(match->post);
@@ -21,6 +23,7 @@ RelocMatch *reloc_match(char *haystack, const char *needle) {
}
reloc_error = RELOC_ESUCCESS;
+ free(pattern);
return match;
}
@@ -42,14 +45,14 @@ RelocData *reloc_read(const char *filename) {
size = ftell(fp);
rewind(fp);
- if (!(data = calloc(sizeof(char), size + 1))) {
+ if (!(data = (char *)calloc(size + 1, sizeof(char)))) {
reloc_error = RELOC_ENOMEM;
return NULL;
}
// Read data into array
fread(data, sizeof(char), size, fp);
- if (!(result = (RelocData *)malloc(sizeof(RelocData)))) {
+ if (!(result = (RelocData *)calloc(1, sizeof(RelocData)))) {
reloc_error = RELOC_ENOMEM;
return NULL;
}
@@ -87,6 +90,9 @@ void reloc_deinit_data(RelocData *finfo) {
if (finfo->path) {
free(finfo->path);
}
+ if (finfo->data) {
+ free(finfo->data);
+ }
free(finfo);
}
}