diff options
author | Paulo Cesar Pereira de Andrade <pcpa@mandriva.com.br> | 2008-03-11 21:52:58 -0300 |
---|---|---|
committer | Paulo Cesar Pereira de Andrade <pcpa@mandriva.com.br> | 2008-07-02 19:07:18 -0300 |
commit | ee636f8565931f8d897b6c8c07eb08d41695778c (patch) | |
tree | 57dd7691a8d8a6079c274252693b2f02e6a9f3f5 | |
parent | 5bd8082a9788a1b5343eb03400944a03e4250577 (diff) |
Fix an incorrect buffer size calculation and allocation.
-rw-r--r-- | lisp/io.c | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -633,26 +633,34 @@ LispFwrite(LispFile *file, void *data, int size) int LispSwrite(LispString *string, void *data, int size) { + int bytes; + if (size < 0) return (EOF); if (string->output + size >= string->space) { if (string->fixed) { /* leave space for a ending nul character */ - size = string->space - string->output - 1; + bytes = string->space - string->output - 1; + + if (bytes < size) + size = bytes; if (size <= 0) return (-1); } else { - char *tmp = realloc(string->string, string->space + - (size / pagesize) * pagesize + pagesize); + char *tmp; + + bytes = string->space + size; + bytes += pagesize - (bytes % pagesize); + tmp = realloc(string->string, bytes); if (tmp == NULL) return (-1); string->string = tmp; - string->space += pagesize; + string->space = bytes; } } memcpy(string->string + string->output, data, size); |