Ethereum Common Pitfalls Discussion

Welcome to the thread about common pitfalls on Ethereum. Here you can discuss everything about that chapter and potential questions that you might have.

How does one append to a string in solidity? Is this not possible, or does a new object need to be constructed each time with the combined string results? I’m currently on the Contracts, Functions and State Vars section… so still early in learning but figure my question may be applicable here.

Basically I am trying to just output a single string given number of iterations provided to the function. Heres what it currently looks like (w/ the assumption the ‘+=’ was an overloaded operator… which is wrong):

function revEngine(uint numRevs) public returns (string noise) {
    for (uint i = 0; i < numRevs; i++) {
        noise += "vrooom ";
    }
    return noise;
}
1 Like

So just got to the Arrays, Structs and Mappings and it appears that the solution to my question above is to have a string array in order to model appended string behavior. @filip Is this the appropriate way to handle such things?

Hi @doon. Good question. I would use a library as https://github.com/Arachnid/solidity-stringutils#concatenating-strings. If you want to do it yourself you would have to manually copy the bytes from the string to the new one.

1 Like

Oh joy lol! Thanks for the info man. I haven’t had to do manual copying to concat a string in a long time, always a pain and takes forever to do something simple (relatively). The library linked definitely has lots of useful string based operations so thanks for that.

Do you know if functions like those in the lib (or similar) will actually become standard in Solidity? And if not, are there some specific reasons as to why from a technical/architecture perspective?

In the quiz, question

Why shouldn’t we perform loops in solidity without a fixed number of interactions?

I choose

We don’t know how much gas that transaction will consume, which can lead to unplanned function behaviour.

and it is marked as the wrong answer but that is sort of what is stated at 7:25

with the difference being the word “interactions” vs. “iterations”, not sure if this was some kind of trick question or just an error, please clarify

2 Likes

Thanks once again. You are completely right. The right answer is “We don’t know how much gas that transaction will consume, which can lead to unplanned function behaviour.”. I have corrected it now.

2 Likes

Re-Entrancy sounds interesting. It would be very nice to see a working example how re-Entrancy works.
It is possible that you share a smart-contract code that call withdraw function several times on the course contract ?

1 Like

I should have included examples in the lecture. You will find examples if you google it. Here is one https://ethereum.stackexchange.com/questions/12465/simple-re-entrance-attack-example-with-fallback-function

3 Likes

combining programming with finance, is the scariest part for me.
Thanks for all the tips

GreetZz

3 Likes

Re-entrancy = DAO hack?

Yes, correct. So very important to know of. Most projects don’t do that mistake today though. Lesson learned…

Shouldn’t the right answer be that we can run out of ether also ? Because if we loop enough, we will run out of ether eventually. Or what do I miss here?

The answer you mention is: “The contract might run out of ether due to the heavy computation that is required.”

Unfortunately it’s not correct because it talks about the contract running out of ether, which is not related to the transaction or the gas spending.

So the only correct answer is: “We don’t know how much gas that transaction will consume, which can lead to unplanned function behaviour.”

Hi Filip,

Now that I’m learning how to program in Solidity I can get a feeling of how hard it must be for you to keep track of all the changes in the documentation and versions of Solidity programming language.

On the part of Common Pitfalls, Tips & Tricks you will have the Solidity v.0.4.0 and there is already a new version on v.0.5.1 >> here’s the new link for the same topic:

https://solidity.readthedocs.io/en/v0.5.1/security-considerations.html

or

https://solidity.readthedocs.io/en/v0.5.1/security-considerations.html#pitfalls

Yes, we are always working to keep our lectures up to date and update the videos on an ongoing basis. But it is impossible to keep up with every version. I would recommend you as well to check out the ethereum smart contract security course, that is done in v5.