Magento 2 – Unable to Serialize Value String Corrupted

magento2

Hi i am trying to run upgrade command but it is giving me this error and due to this error my admin panel and front end both are showing error to run upgrade command, whenever i run this command :

php bin/magento setup:upgrade

it gives me error on cmd :

enter image description here

Although i am in developer mode and already run di:compile command many times along with cleaning cache,
can anyone guide me please what's creating issue and quick solution to fix it ,Thanks.

—————-edit ————–

i have pasted following code in json.php

public function unserialize($string)
{
    if($this->serialize($string))
    {
        $string = $this->serialize($string);
    }
    $result = json_decode($string, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
         throw new \InvalidArgumentException('Unable to unserialize value.');

    }
    return $result;
}

public function serialize($value, &$result = null)
{
    // Bit of a give away this one
    if (!is_string($value))
    {
        return false;
    }
    // Serialized false, return true. unserialize() returns false on an
    // invalid string or it could return false if the string is serialized
    // false, eliminate that possibility.
    if ($value === 'b:0;')
    {
        $result = false;
        return true;
    }
    $length = strlen($value);
    $end    = '';
    switch ($value[0])
    {
        case 's':
            if ($value[$length - 2] !== '"')
            {
                return false;
            }
        case 'b':
        case 'i':
        case 'd':
            // This looks odd but it is quicker than isset()ing
            $end .= ';';
        case 'a':
        case 'O':
            $end .= '}';
            if ($value[1] !== ':')
            {
                return false;
            }
            switch ($value[2])
            {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    break;
                default:
                    return false;
            }
        case 'N':
            $end .= ';';
            if ($value[$length - 1] !== $end[0])
            {
                return false;
            }
            break;
        default:
            return false;
    }
    if (($result = @unserialize($value)) === false)
    {
        $result = null;
        return false;
    }
    return true;
}

i search it on internet its because of older version issue , i am currently using 2.2.0 magento version. but still getting another error

Best Answer

I am not sure but problem may be in your module Vnecoms_SmsTeleSign

As per Magento 2.2 Release Note

Security enhancements

In general, we’ve removed serialize/unserialize from most the code to improve protection against remote code execution attacks. We’ve enhanced protection of code where use of object serialization or unserialization was unavoidable. Additionally, we’ve increased our use of output escaping to protect against cross-site scripting (XSS) attacks.

So you need to inject \Magento\Framework\Serialize\Serializer\Json class for serialize and unserialize values.

use following code for serialize and unserialize values.

protected $serialize;

public function __construct(
    \Magento\Framework\Serialize\Serializer\Json $serialize
) {
    $this->serialize = $serialize;
}

public function serialize($value){
    return $this->serialize->serialize($value);
}

public function unserialize($value){
    return $this->serialize->unserialize($value);
}