Skip to content

Commit 6ef58da

Browse files
authored
ext/mysqlnd: Refactor usage of strlcpy() (#17185)
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()`
1 parent 65524e5 commit 6ef58da

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

ext/mysqlnd/mysqlnd_loaddata.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ static
6565
int mysqlnd_local_infile_read(void * ptr, zend_uchar * buf, unsigned int buf_len)
6666
{
6767
MYSQLND_INFILE_INFO *info = (MYSQLND_INFILE_INFO *)ptr;
68-
int count;
6968

7069
DBG_ENTER("mysqlnd_local_infile_read");
7170

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

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

9292
if (info) {
93-
strlcpy(error_buf, info->error_msg, error_buf_len);
93+
size_t error_msg_len_with_null_byte = strlen(info->error_msg) + 1;
94+
ZEND_ASSERT(error_buf_len >= error_msg_len_with_null_byte);
95+
96+
memcpy(error_buf, info->error_msg, error_msg_len_with_null_byte);
9497
DBG_INF_FMT("have info, %d", info->error_no);
9598
DBG_RETURN(info->error_no);
9699
}
97100

98-
strlcpy(error_buf, "Unknown error", error_buf_len);
101+
ZEND_ASSERT(error_buf_len >= sizeof("Unknown error"));
102+
strcpy(error_buf, "Unknown error");
99103
DBG_INF_FMT("no info, %d", CR_UNKNOWN_ERROR);
100104
DBG_RETURN(CR_UNKNOWN_ERROR);
101105
}

0 commit comments

Comments
 (0)