Commit c1581854 authored by Niels Möller's avatar Niels Möller

* examples/io.c (read_file): Bug fix, used to overwrite pointer.

Rev: src/nettle/examples/io.c:1.2
parent 577e1b4f
...@@ -52,17 +52,18 @@ werror(const char *format, ...) ...@@ -52,17 +52,18 @@ werror(const char *format, ...)
#define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MIN(a,b) (((a) < (b)) ? (a) : (b))
unsigned unsigned
read_file(const char *name, unsigned max_size, char **buffer) read_file(const char *name, unsigned max_size, char **contents)
{ {
unsigned size; unsigned size;
unsigned done; unsigned done;
char *buffer;
FILE *f; FILE *f;
f = fopen(name, "rb"); f = fopen(name, "rb");
if (!f) if (!f)
return 0; return 0;
*buffer = NULL; buffer = NULL;
if (max_size && max_size < 100) if (max_size && max_size < 100)
size = max_size; size = max_size;
...@@ -75,22 +76,22 @@ read_file(const char *name, unsigned max_size, char **buffer) ...@@ -75,22 +76,22 @@ read_file(const char *name, unsigned max_size, char **buffer)
{ {
char *p; char *p;
if (size > max_size) if (max_size && size > max_size)
size = max_size; size = max_size;
/* Space for terminating NUL */ /* Space for terminating NUL */
p = realloc(*buffer, size + 1); p = realloc(buffer, size + 1);
if (!p) if (!p)
{ {
fail: fail:
fclose(f); fclose(f);
free(*buffer); free(buffer);
*buffer = NULL; *contents = NULL;
return 0; return 0;
} }
*buffer = p; buffer = p;
done += fread(buffer + done, 1, size - done, f); done += fread(buffer + done, 1, size - done, f);
if (ferror(f)) if (ferror(f))
...@@ -100,7 +101,9 @@ read_file(const char *name, unsigned max_size, char **buffer) ...@@ -100,7 +101,9 @@ read_file(const char *name, unsigned max_size, char **buffer)
fclose(f); fclose(f);
/* NUL-terminate the data. */ /* NUL-terminate the data. */
(*buffer)[done] = '\0'; buffer[done] = '\0';
*contents = buffer;
return done; return done;
} }
......
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