Skip to content

Commit 1827a77

Browse files
committed
Remove 64kb interactive line limit of 'duk'
1 parent bc9a274 commit 1827a77

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

examples/cmdline/duk_cmdline.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103

104104
#define MEM_LIMIT_NORMAL (128*1024*1024) /* 128 MB */
105105
#define MEM_LIMIT_HIGH (2047*1024*1024) /* ~2 GB */
106-
#define LINEBUF_SIZE 65536
107106

108107
static int main_argc = 0;
109108
static char **main_argv = NULL;
@@ -545,6 +544,9 @@ static int handle_fh(duk_context *ctx, FILE *f, const char *filename, const char
545544
fprintf(stderr, "resizing read buffer: %ld -> %ld\n", (long) bufsz, (long) (bufsz * 2));
546545
#endif
547546
newsz = bufsz + (bufsz >> 2) + 1024; /* +25% and some extra */
547+
if (newsz < bufsz) {
548+
goto error;
549+
}
548550
buf_new = (char *) realloc(buf, newsz);
549551
if (!buf_new) {
550552
goto error;
@@ -789,37 +791,42 @@ static int handle_interactive(duk_context *ctx) {
789791
#else /* DUK_CMDLINE_LINENOISE */
790792
static int handle_interactive(duk_context *ctx) {
791793
const char *prompt = "duk> ";
794+
size_t bufsize = 0;
792795
char *buffer = NULL;
793796
int retval = 0;
794797
int rc;
795798
int got_eof = 0;
796799

797-
buffer = (char *) malloc(LINEBUF_SIZE);
798-
if (!buffer) {
799-
fprintf(stderr, "failed to allocated a line buffer\n");
800-
fflush(stderr);
801-
retval = -1;
802-
goto done;
803-
}
804-
805800
while (!got_eof) {
806801
size_t idx = 0;
807802

808803
fwrite(prompt, 1, strlen(prompt), stdout);
809804
fflush(stdout);
810805

811806
for (;;) {
812-
int c = fgetc(stdin);
807+
int c;
808+
809+
if (idx >= bufsize) {
810+
size_t newsize = bufsize + (bufsize >> 2) + 1024; /* +25% and some extra */
811+
char *newptr;
812+
813+
if (newsize < bufsize) {
814+
goto fail_realloc;
815+
}
816+
newptr = (char *) realloc(buffer, newsize);
817+
if (!newptr) {
818+
goto fail_realloc;
819+
}
820+
buffer = newptr;
821+
bufsize = newsize;
822+
}
823+
824+
c = fgetc(stdin);
813825
if (c == EOF) {
814826
got_eof = 1;
815827
break;
816828
} else if (c == '\n') {
817829
break;
818-
} else if (idx >= LINEBUF_SIZE) {
819-
fprintf(stderr, "line too long\n");
820-
fflush(stderr);
821-
retval = -1;
822-
goto done;
823830
} else {
824831
buffer[idx++] = (char) c;
825832
}
@@ -853,6 +860,12 @@ static int handle_interactive(duk_context *ctx) {
853860
}
854861

855862
return retval;
863+
864+
fail_realloc:
865+
fprintf(stderr, "failed to extend line buffer\n");
866+
fflush(stderr);
867+
retval = -1;
868+
goto done;
856869
}
857870
#endif /* DUK_CMDLINE_LINENOISE */
858871

0 commit comments

Comments
 (0)