1

I use this function to convert type uint256 to string.But i get error message about "Security/No-assign-param"

function converIntToString(uint number) internal view returns(string){

    if (number == 0) 
        return "0";
    uint j = number;
    uint length;
    while (j != 0){
        length++;
        j /= 10;
    }

    bytes memory bstr = new bytes(length);
    uint k = length - 1;
    while (number != 0){
        bstr[k--] = byte(48 + number % 10);
        number /= 10;
    }

    return string(bstr);

}

I am using VSCode and I did not encounter the same problem on Remix IDE

Is there anyone who can help me solve this problem?

3

This warning appear when you use the parameter and calculate inside while loop, for suppress one workaround this warning you can use one temporary variable like below:

function convertIntToString(uint256 _number) 
internal 
pure 
returns (string) {
    uint256 _tmpN = _number;

    if (_tmpN == 0) {
        return "0";
    } 

    uint256 j = _tmpN;
    uint256 length = 0;

    while (j != 0){
        length++;
        j /= 10;
    }

    bytes memory bstr = new bytes(length);

    uint256 k = length - 1;

    while (_tmpN != 0) {
        bstr[k--] = byte(48 + _tmpN % 10);
        _tmpN /= 10;
    }

    return string(bstr);

}

your function can have pure and not view because its based on parameters and not read state from smart contract.

| improve this answer | |
3

Why you're seeing this error

If you use vscode with vscode-solidity extension, it comes with solium as linter and security ruleset is enabled by default.

If you are to read the solium-plugin-security README, you will notice no-assign-params means Disallow assigning to function parameters.

But nontheless the contract should still compile if you run solc.

Getting rid of the error

You can either fix this by

  • Disable the linter in vscode-solidity altogether: Set "solidity.linter": false in your vscode user preferences.
  • Disable the security/no-assign-params rule in either the file, or the line. Please refer to solium documentation
  • Fix the security problem pointed out by solium. Make a copy of the param (uint number), so you don't assign value to it in number /= 10
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.