Skip to content

Commit

Permalink
ext/mysqlnd: Refactor usage of strlcpy() (#17185)
Browse files Browse the repository at this point in the history
The two calls that MySQLnd does to this handler all pass a buffer the same size as the error_msg field
Thus, we know that we can just memcpy the error message into the buffer.

See https://nrk.neocities.org/articles/not-a-fan-of-strlcpy for a rationale against the usage of `strlcpy()`
  • Loading branch information
Girgias authored Dec 28, 2024
1 parent 65524e5 commit 6ef58da
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ext/mysqlnd/mysqlnd_loaddata.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ static
int mysqlnd_local_infile_read(void * ptr, zend_uchar * buf, unsigned int buf_len)
{
MYSQLND_INFILE_INFO *info = (MYSQLND_INFILE_INFO *)ptr;
int count;

DBG_ENTER("mysqlnd_local_infile_read");

count = (int) php_stream_read(info->fd, (char *) buf, buf_len);
// TODO Change this, and the return type of the function to ssize_t
int count = (int) php_stream_read(info->fd, (char *) buf, buf_len);

if (count < 0) {
strcpy(info->error_msg, "Error reading file");
Expand All @@ -90,12 +90,16 @@ int mysqlnd_local_infile_error(void * ptr, char *error_buf, unsigned int error_b
DBG_ENTER("mysqlnd_local_infile_error");

if (info) {
strlcpy(error_buf, info->error_msg, error_buf_len);
size_t error_msg_len_with_null_byte = strlen(info->error_msg) + 1;
ZEND_ASSERT(error_buf_len >= error_msg_len_with_null_byte);

memcpy(error_buf, info->error_msg, error_msg_len_with_null_byte);
DBG_INF_FMT("have info, %d", info->error_no);
DBG_RETURN(info->error_no);
}

strlcpy(error_buf, "Unknown error", error_buf_len);
ZEND_ASSERT(error_buf_len >= sizeof("Unknown error"));
strcpy(error_buf, "Unknown error");
DBG_INF_FMT("no info, %d", CR_UNKNOWN_ERROR);
DBG_RETURN(CR_UNKNOWN_ERROR);
}
Expand Down

0 comments on commit 6ef58da

Please sign in to comment.