Commit 501f8c86 authored by Nikias Bassen's avatar Nikias Bassen

plistutil: Fix stdin input buffer reallocation

parent ced16994
...@@ -34,8 +34,6 @@ ...@@ -34,8 +34,6 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#define BUF_SIZE 2048 // Seems to be a decent start to cover most stdin files
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable:4996) #pragma warning(disable:4996)
#endif #endif
...@@ -147,6 +145,7 @@ int main(int argc, char *argv[]) ...@@ -147,6 +145,7 @@ int main(int argc, char *argv[])
char *plist_out = NULL; char *plist_out = NULL;
uint32_t size = 0; uint32_t size = 0;
int read_size = 0; int read_size = 0;
int read_capacity = 4096;
char *plist_entire = NULL; char *plist_entire = NULL;
struct stat filestats; struct stat filestats;
options_t *options = parse_arguments(argc, argv); options_t *options = parse_arguments(argc, argv);
...@@ -160,7 +159,7 @@ int main(int argc, char *argv[]) ...@@ -160,7 +159,7 @@ int main(int argc, char *argv[])
if (!options->in_file || !strcmp(options->in_file, "-")) if (!options->in_file || !strcmp(options->in_file, "-"))
{ {
read_size = 0; read_size = 0;
plist_entire = malloc(sizeof(char) * BUF_SIZE); plist_entire = malloc(sizeof(char) * read_capacity);
if(plist_entire == NULL) if(plist_entire == NULL)
{ {
printf("ERROR: Failed to allocate buffer to read from stdin"); printf("ERROR: Failed to allocate buffer to read from stdin");
...@@ -171,9 +170,10 @@ int main(int argc, char *argv[]) ...@@ -171,9 +170,10 @@ int main(int argc, char *argv[])
char ch; char ch;
while(read(STDIN_FILENO, &ch, 1) > 0) while(read(STDIN_FILENO, &ch, 1) > 0)
{ {
if (read_size >= BUF_SIZE) { if (read_size >= read_capacity) {
char *old = plist_entire; char *old = plist_entire;
plist_entire = realloc(plist_entire, sizeof(char) * (read_size + 1)); read_capacity += 4096;
plist_entire = realloc(plist_entire, sizeof(char) * read_capacity);
if (plist_entire == NULL) if (plist_entire == NULL)
{ {
printf("ERROR: Failed to reallocate stdin buffer\n"); printf("ERROR: Failed to reallocate stdin buffer\n");
...@@ -185,6 +185,17 @@ int main(int argc, char *argv[]) ...@@ -185,6 +185,17 @@ int main(int argc, char *argv[])
plist_entire[read_size] = ch; plist_entire[read_size] = ch;
read_size++; read_size++;
} }
if (read_size >= read_capacity) {
char *old = plist_entire;
plist_entire = realloc(plist_entire, sizeof(char) * (read_capacity+1));
if (plist_entire == NULL)
{
printf("ERROR: Failed to reallocate stdin buffer\n");
free(old);
free(options);
return 1;
}
}
plist_entire[read_size] = '\0'; plist_entire[read_size] = '\0';
// Not positive we need this, but it doesnt seem to hurt lol // Not positive we need this, but it doesnt seem to hurt lol
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment