readfile NUL terminates the buffer to make things easy.
We need to transmit this over IPC just ensure the receiver has
a big enough buffer, again to make things easy.
So on success, we need to trim the NUL from the returned length
so the actual file size is accurate.
return -1;
if (fstat(fd, &st) == -1)
goto out;
return -1;
if (fstat(fd, &st) == -1)
goto out;
+
+ /* Ensure that what we read is NUL terminated
+ * because it's mainly text files and this just
+ * makes it easier. */
nlen = (size_t)st.st_size + 1;
if (nlen > *len) {
void *ndata = realloc(*data, nlen);
nlen = (size_t)st.st_size + 1;
if (nlen > *len) {
void *ndata = realloc(*data, nlen);
*data = ndata;
*len = nlen;
}
*data = ndata;
*len = nlen;
}
- bytes = read(fd, *data, *len);
+ bytes = read(fd, *data, *len - 1);
+
out:
close(fd);
if (bytes == -1)
return -1;
buf = *data;
buf[bytes] = '\0';
out:
close(fd);
if (bytes == -1)
return -1;
buf = *data;
buf[bytes] = '\0';
err = readfile(data, &ctx->ps_buf, &ctx->ps_buflen);
if (err != -1) {
rdata = ctx->ps_buf;
err = readfile(data, &ctx->ps_buf, &ctx->ps_buflen);
if (err != -1) {
rdata = ctx->ps_buf;
+ /* We know the buffer is NUL terminated.
+ * Send it over IPC to ensure the receiver has
+ * enough space for it as well. */
+ rlen = (size_t)err + 1;
}
break;
case PS_WRITEFILE:
}
break;
case PS_WRITEFILE:
ps_root_readfile(struct dhcpcd_ctx *ctx, const char *file, void **data,
size_t *len)
{
ps_root_readfile(struct dhcpcd_ctx *ctx, const char *file, void **data,
size_t *len)
{
if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_READFILE, 0, file,
strlen(file) + 1) == -1)
return -1;
if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_READFILE, 0, file,
strlen(file) + 1) == -1)
return -1;
- return ps_root_mreaderror(ctx, data, len);
+ err = ps_root_mreaderror(ctx, data, len);
+ if (err == -1)
+ return -1;
+
+ /* The returned length should not include the NUL terminator */
+ return err - 1;