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;
}
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityWhy 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;
}
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.