1.1 --- a/templates/native/io.c Mon Dec 12 00:33:33 2016 +0100
1.2 +++ b/templates/native/io.c Mon Dec 12 00:37:28 2016 +0100
1.3 @@ -17,7 +17,7 @@
1.4 */
1.5
1.6 #include <unistd.h> /* read, write */
1.7 -#include <string.h> /* strcmp, strncpy, strlen */
1.8 +#include <string.h> /* strcmp, memcpy */
1.9 #include <stdio.h> /* fdopen, snprintf */
1.10 #include <errno.h> /* errno */
1.11 #include "native/common.h"
1.12 @@ -128,8 +128,8 @@
1.13 /* Reserve space for a new string. */
1.14
1.15 s = __ALLOCATE(have_read + 1, sizeof(char));
1.16 - strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
1.17 - return __new_str(s);
1.18 + memcpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
1.19 + return __new_str(s, have_read);
1.20 }
1.21
1.22 __attr __fn_native_io_fwrite(__attr __args[])
1.23 @@ -139,8 +139,9 @@
1.24 /* fp interpreted as FILE reference */
1.25 FILE *f = (FILE *) fp->datavalue;
1.26 /* str.__data__ interpreted as string */
1.27 - char *s = __load_via_object(str->value, __pos___data__).strvalue;
1.28 - size_t to_write = strlen(s);
1.29 + __attr sa = __load_via_object(str->value, __pos___data__);
1.30 + char *s = sa.strvalue;
1.31 + size_t to_write = sa.size;
1.32 size_t have_written = fwrite(s, sizeof(char), to_write, f);
1.33 int error;
1.34
1.35 @@ -189,8 +190,8 @@
1.36 /* Reserve space for a new string. */
1.37
1.38 s = __ALLOCATE(have_read + 1, 1);
1.39 - strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
1.40 - return __new_str(s);
1.41 + memcpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
1.42 + return __new_str(s, have_read);
1.43 }
1.44
1.45 __attr __fn_native_io_write(__attr __args[])
1.46 @@ -200,11 +201,12 @@
1.47 /* fd.__data__ interpreted as int */
1.48 int i = __load_via_object(fd->value, __pos___data__).intvalue;
1.49 /* str.__data__ interpreted as string */
1.50 - char *s = __load_via_object(str->value, __pos___data__).strvalue;
1.51 + __attr sa = __load_via_object(str->value, __pos___data__);
1.52 + char *s = sa.strvalue;
1.53 ssize_t have_written;
1.54
1.55 errno = 0;
1.56 - have_written = write(i, s, sizeof(char) * strlen(s));
1.57 + have_written = write(i, s, sizeof(char) * sa.size);
1.58
1.59 if (have_written == -1)
1.60 __raise_io_error(__new_int(errno));