Skip to content

Commit

Permalink
Allocate receive buffer on stack instead of heap
Browse files Browse the repository at this point in the history
Changed to code in `CopyRegularFileNet` to allocate the receive buffer
on the stack instead of the heap. There is no reason to have it
dynamically allocated since the size is constant. Furthermore, the
buffer is allocated and destroyed in the same scope.

Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Oct 28, 2024
1 parent e113e8e commit 33684f1
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions libcfnet/client_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,8 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
{
assert(conn != NULL);

char *buf, workbuf[CF_BUFSIZE], cfchangedstr[265];
char buf[CF_BUFSIZE + sizeof(int)]; /* Note CF_BUFSIZE not buf_size !! */
char workbuf[CF_BUFSIZE], cfchangedstr[265];
const int buf_size = 2048;

/* We encrypt only for CLASSIC protocol. The TLS protocol is always over
Expand Down Expand Up @@ -793,8 +794,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
return false;
}

buf = xmalloc(CF_BUFSIZE + sizeof(int)); /* Note CF_BUFSIZE not buf_size !! */

Log(LOG_LEVEL_VERBOSE, "Copying remote file '%s:%s', expecting %jd bytes",
conn->this_server, source, (intmax_t)size);

Expand Down Expand Up @@ -838,7 +837,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
conn->this_server, source, n_read);

close(dd);
free(buf);
return false;
}

Expand All @@ -850,7 +848,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
Log(LOG_LEVEL_INFO, "Network access to '%s:%s' denied",
conn->this_server, source);
close(dd);
free(buf);
return false;
}

Expand All @@ -859,7 +856,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
Log(LOG_LEVEL_INFO, "Source '%s:%s' changed while copying",
conn->this_server, source);
close(dd);
free(buf);
return false;
}

Expand All @@ -877,7 +873,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
Log(LOG_LEVEL_INFO, "Network access to cleartext '%s:%s' denied",
conn->this_server, source);
close(dd);
free(buf);
return false;
}

Expand All @@ -888,7 +883,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
Log(LOG_LEVEL_ERR,
"Local disk write failed copying '%s:%s' to '%s'",
conn->this_server, source, dest);
free(buf);
unlink(dest);
close(dd);
FlushFileStream(conn->conn_info->sd, size - n_wrote_total - n_read);
Expand All @@ -908,7 +902,6 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
unlink(dest);
FlushFileStream(conn->conn_info->sd, size - n_wrote_total);
close(dd);
free(buf);
return false;
}
}
Expand All @@ -920,11 +913,9 @@ bool CopyRegularFileNet(const char *source, const char *dest, off_t size,
if (!ret)
{
unlink(dest);
free(buf);
FlushFileStream(conn->conn_info->sd, size - n_wrote_total);
return false;
}

free(buf);
return true;
}

0 comments on commit 33684f1

Please sign in to comment.