Skip to content

Commit a716235

Browse files
committed
Fix race condition bug with large piped input.
modified: src/mods/config.h modified: src/mods/input.c
1 parent 0e4014d commit a716235

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/mods/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
#define BTK_VERSION_MAJOR 2
1212
#define BTK_VERSION_MINOR 0
13-
#define BTK_VERSION_REVISION 1
13+
#define BTK_VERSION_REVISION 2
1414

1515
#endif

src/mods/input.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,25 +229,45 @@ int input_get_str(char** dest, char *prompt)
229229

230230
int input_get_from_pipe(unsigned char** dest)
231231
{
232-
int r;
232+
int r, size = 0, prev_size = 0;
233+
unsigned char *tmp;
233234

234235
if (isatty(STDIN_FILENO))
235236
{
236237
error_log("Input data from a piped or redirected source is required.");
237238
return -1;
238239
}
239240

240-
r = input_get(dest, NULL, INPUT_GET_MODE_ALL);
241-
if (r < 0)
242-
{
243-
error_log("Could not get input.");
244-
return -1;
245-
}
246-
if (r == 0)
241+
while (input_available())
247242
{
248-
error_log("No input provided.");
249-
return -1;
243+
r = input_get(&tmp, NULL, INPUT_GET_MODE_ALL);
244+
if (r < 0)
245+
{
246+
error_log("Could not get input.");
247+
return -1;
248+
}
249+
if (r == 0)
250+
{
251+
error_log("No input provided.");
252+
return -1;
253+
}
254+
255+
size += r;
256+
257+
(*dest) = realloc((*dest), size);
258+
if ((*dest) == NULL)
259+
{
260+
error_log("Memory allocation error.");
261+
return -1;
262+
}
263+
264+
memcpy((*dest) + prev_size, tmp, r);
265+
266+
prev_size = size;
267+
268+
free(tmp);
269+
tmp = NULL;
250270
}
251271

252-
return r;
272+
return size;
253273
}

0 commit comments

Comments
 (0)