0

I've seen other similar questions but they don't explain why this error happens and/or they are case specific questions. So, I have this code:

pragma solidity >=0.4.22 <0.7.0;


contract MagicIdeas {

    struct IdeaCard {
        address owner;
        string title;
        string text;
        string img_url;
        string card_id;
    }

    ....

    function update_card_data(string memory _card_id,
                            string memory _new_title,
                            string memory _new_text,
                            string memory _new_img_url) public {
        IdeaCard storage updatedCard; // ERROR HERE
        updatedCard = IdCardMap[_card_id];
        if(bytes(_new_title).length>0){
            updatedCard.title = _new_title;
        }
        if(bytes(_new_text).length>0){
            updatedCard.text = _new_text;
        }
        if(bytes(_new_img_url).length>0){
            updatedCard.img_url = _new_img_url;
        }
    }

Whe I run truffle test I get the following:

DeclarationError: Uninitialized storage pointer.
        IdeaCard storage updatedCard;
        ^--------------------------^

Why does this happen? How can I solve it?

Extra: The same code compiles correctly on remix.

1

Storage references will always point to a valid storage slot, even whey they are uninitialized.

So something like this

IdeaCard storage updatedCard;
updatedCard.owner = 0x1234..1234;

worked in solc 0.4 and caused overwriting valid storage slots.

In solc 0.5 it was turned into an error, but in solc 0.6 it is again a warning. Perhaps Truffle is compiling with warnings turned into errors and it make the compilation to fail.


In your example it should be easy to solve by combining two lines into one:

IdeaCard storage updatedCard = IdCardMap[_card_id];
  • 1
    Detecting uninitialized storage pointer seems to be introduced in 0.4.24. The pragma says >=0.4.22 <0.7.0. A possible explanation for remix's behaviour is that it's getting compiled on 0.4.22 or 0.4.23. – hrkrshnn Jul 26 '20 at 18:10

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.