6

Why does the require not throw here when I am passing an empty string for the _name param?

function MyContract(bytes32 _name) public {
        require(_name.length > 0);
        name = _name;
    }
8

The bytes32 type is always exactly 32 bytes. Therefore, its length is always 32. It is unaware of whether it contains a string, number or something else.

I would recommend doing:

require(_name[0] != 0);

to verify that it does not represent an empty string.

| improve this answer | |
  • 1
    Note: This only works for null-terminated strings. – Jesbus Dec 16 '19 at 18:32

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.