Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext/mysqlnd: Refactor usage of strlcpy() #17185

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ext/mysqlnd/mysqlnd_loaddata.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ 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);
ssize_t count = php_stream_read(info->fd, (char *) buf, buf_len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of int mysqlnd_local_infile_read() should be changed as well, otherwise the count < 0 check bellow may be false, but the return value may still be < 0 due to conversion to int.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the conversion to int take place?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens when returning count, as the return type is int

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an unrelated change so it should be probably removed from this PR and different PR created for this.

Girgias marked this conversation as resolved.
Show resolved Hide resolved

if (count < 0) {
strcpy(info->error_msg, "Error reading file");
Expand All @@ -90,12 +89,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);
kamil-tekiela marked this conversation as resolved.
Show resolved Hide resolved

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
Loading