@@ -340,7 +340,7 @@ static struct tag *last_tag;
340340
341341/* Input stream parsing */
342342static whenspec_type whenspec = WHENSPEC_RAW ;
343- static struct strbuf command_buf ;
343+ static struct strbuf command_buf = STRBUF_INIT ;
344344static int unread_command_buf ;
345345static struct recent_command cmd_hist = {& cmd_hist , & cmd_hist , NULL };
346346static struct recent_command * cmd_tail = & cmd_hist ;
@@ -1638,39 +1638,35 @@ static void cmd_mark(void)
16381638
16391639static void * cmd_data (size_t * size )
16401640{
1641- size_t length ;
1642- char * buffer ;
1641+ struct strbuf buffer ;
16431642
1643+ strbuf_init (& buffer );
16441644 if (prefixcmp (command_buf .buf , "data " ))
16451645 die ("Expected 'data n' command, found: %s" , command_buf .buf );
16461646
16471647 if (!prefixcmp (command_buf .buf + 5 , "<<" )) {
16481648 char * term = xstrdup (command_buf .buf + 5 + 2 );
1649- size_t sz = 8192 , term_len = command_buf .len - 5 - 2 ;
1650- length = 0 ;
1651- buffer = xmalloc (sz );
1649+ size_t term_len = command_buf .len - 5 - 2 ;
1650+
16521651 for (;;) {
16531652 read_line (& command_buf , stdin , '\n' );
16541653 if (command_buf .eof )
16551654 die ("EOF in data (terminator '%s' not found)" , term );
16561655 if (term_len == command_buf .len
16571656 && !strcmp (term , command_buf .buf ))
16581657 break ;
1659- ALLOC_GROW (buffer , length + command_buf .len + 1 , sz );
1660- memcpy (buffer + length ,
1661- command_buf .buf ,
1662- command_buf .len );
1663- length += command_buf .len ;
1664- buffer [length ++ ] = '\n' ;
1658+ strbuf_addbuf (& buffer , & command_buf );
1659+ strbuf_addch (& buffer , '\n' );
16651660 }
16661661 free (term );
16671662 }
16681663 else {
1669- size_t n = 0 ;
1664+ size_t n = 0 , length ;
1665+
16701666 length = strtoul (command_buf .buf + 5 , NULL , 10 );
1671- buffer = xmalloc ( length );
1667+
16721668 while (n < length ) {
1673- size_t s = fread ( buffer + n , 1 , length - n , stdin );
1669+ size_t s = strbuf_fread ( & buffer , length - n , stdin );
16741670 if (!s && feof (stdin ))
16751671 die ("EOF in data (%lu bytes remaining)" ,
16761672 (unsigned long )(length - n ));
@@ -1679,8 +1675,8 @@ static void *cmd_data (size_t *size)
16791675 }
16801676
16811677 skip_optional_lf ();
1682- * size = length ;
1683- return buffer ;
1678+ * size = buffer . len ;
1679+ return strbuf_detach ( & buffer ) ;
16841680}
16851681
16861682static int validate_raw_date (const char * src , char * result , int maxlen )
0 commit comments