MyBB Community Forums

Full Version: MySQL error with strict mode if message too long
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
If the message is longer than the limit of the message field in the database, with MySQL strict mode enabled it throws an error instead of cutting the message after the limit.

SQL Error:
1406 - Data too long for column 'message' at row 1

To avoid this error in our forum, I added in function verify_message() in inc/datahandlers/post.php below

// If this board has a maximum message length check if we're over it. Use strlen because SQL limits are in bytes
else if(strlen($post['message']) > $mybb->settings['maxmessagelength'] && $mybb->settings['maxmessagelength'] > 0 && !is_moderator($post['fid'], "", $post['uid']))
{
    $this->set_error("message_too_long", array($mybb->settings['maxmessagelength'], strlen($post['message'])));
    return false;
}

a check:

// If the message length is over SQL limits
else if(strlen($post['message']) > 65535)
{
    global $db;

    $fieldlength = $db->show_fields_from("posts");
    if(is_array($fieldlength))
    {
        foreach ($fieldlength as $key => $fields)
        {
            if($fields['Field'] == 'message')
            {
                switch($fields['Type'])
                {
                    case 'text':
                    {
                        $maximum = 65535;
                    }
                    break;
                    case 'mediumtext':
                    {
                        $maximum = 16777215;
                    }
                    break;
                    case 'longtext':
                    {
                        $maximum = 4294967295;
                    }
                    break;
                    default:
                        $maximum = 65535;
                }
            }
        }
    }
    if(isset($maximum) && strlen($post['message']) > $maximum)
    {
        $this->set_error("message_too_long", array($maximum, strlen($post['message'])));
        return false;
    }
}
@doylecc - thank you, I can confirm that on my test board (enabled MyYQL strict mode).
Hi,

Thank you for your report. We have pushed this issue to our Github repository for further analysis where you can track our commits and progress with fixing this bug. Discussions regarding this bug may also take place there too.

Follow this link to visit the issue on Github: https://github.com/mybb/mybb/issues/3170

Thanks for contributing to MyBB!

Regards,
The MyBB Group