diff options
Diffstat (limited to 'tools/plistutil.c')
| -rw-r--r-- | tools/plistutil.c | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/tools/plistutil.c b/tools/plistutil.c index fd0847b..71972f9 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c | |||
| @@ -34,8 +34,6 @@ | |||
| 34 | #include <errno.h> | 34 | #include <errno.h> |
| 35 | #include <unistd.h> | 35 | #include <unistd.h> |
| 36 | 36 | ||
| 37 | #define BUF_SIZE 2048 // Seems to be a decent start to cover most stdin files | ||
| 38 | |||
| 39 | #ifdef _MSC_VER | 37 | #ifdef _MSC_VER |
| 40 | #pragma warning(disable:4996) | 38 | #pragma warning(disable:4996) |
| 41 | #endif | 39 | #endif |
| @@ -147,6 +145,7 @@ int main(int argc, char *argv[]) | |||
| 147 | char *plist_out = NULL; | 145 | char *plist_out = NULL; |
| 148 | uint32_t size = 0; | 146 | uint32_t size = 0; |
| 149 | int read_size = 0; | 147 | int read_size = 0; |
| 148 | int read_capacity = 4096; | ||
| 150 | char *plist_entire = NULL; | 149 | char *plist_entire = NULL; |
| 151 | struct stat filestats; | 150 | struct stat filestats; |
| 152 | options_t *options = parse_arguments(argc, argv); | 151 | options_t *options = parse_arguments(argc, argv); |
| @@ -160,7 +159,7 @@ int main(int argc, char *argv[]) | |||
| 160 | if (!options->in_file || !strcmp(options->in_file, "-")) | 159 | if (!options->in_file || !strcmp(options->in_file, "-")) |
| 161 | { | 160 | { |
| 162 | read_size = 0; | 161 | read_size = 0; |
| 163 | plist_entire = malloc(sizeof(char) * BUF_SIZE); | 162 | plist_entire = malloc(sizeof(char) * read_capacity); |
| 164 | if(plist_entire == NULL) | 163 | if(plist_entire == NULL) |
| 165 | { | 164 | { |
| 166 | printf("ERROR: Failed to allocate buffer to read from stdin"); | 165 | printf("ERROR: Failed to allocate buffer to read from stdin"); |
| @@ -171,9 +170,10 @@ int main(int argc, char *argv[]) | |||
| 171 | char ch; | 170 | char ch; |
| 172 | while(read(STDIN_FILENO, &ch, 1) > 0) | 171 | while(read(STDIN_FILENO, &ch, 1) > 0) |
| 173 | { | 172 | { |
| 174 | if (read_size >= BUF_SIZE) { | 173 | if (read_size >= read_capacity) { |
| 175 | char *old = plist_entire; | 174 | char *old = plist_entire; |
| 176 | plist_entire = realloc(plist_entire, sizeof(char) * (read_size + 1)); | 175 | read_capacity += 4096; |
| 176 | plist_entire = realloc(plist_entire, sizeof(char) * read_capacity); | ||
| 177 | if (plist_entire == NULL) | 177 | if (plist_entire == NULL) |
| 178 | { | 178 | { |
| 179 | printf("ERROR: Failed to reallocate stdin buffer\n"); | 179 | printf("ERROR: Failed to reallocate stdin buffer\n"); |
| @@ -185,6 +185,17 @@ int main(int argc, char *argv[]) | |||
| 185 | plist_entire[read_size] = ch; | 185 | plist_entire[read_size] = ch; |
| 186 | read_size++; | 186 | read_size++; |
| 187 | } | 187 | } |
| 188 | if (read_size >= read_capacity) { | ||
| 189 | char *old = plist_entire; | ||
| 190 | plist_entire = realloc(plist_entire, sizeof(char) * (read_capacity+1)); | ||
| 191 | if (plist_entire == NULL) | ||
| 192 | { | ||
| 193 | printf("ERROR: Failed to reallocate stdin buffer\n"); | ||
| 194 | free(old); | ||
| 195 | free(options); | ||
| 196 | return 1; | ||
| 197 | } | ||
| 198 | } | ||
| 188 | plist_entire[read_size] = '\0'; | 199 | plist_entire[read_size] = '\0'; |
| 189 | 200 | ||
| 190 | // Not positive we need this, but it doesnt seem to hurt lol | 201 | // Not positive we need this, but it doesnt seem to hurt lol |
