<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/introduction-to-solidity-smart-contracts-storage-layout----what-are-risks-in-manipulating-storage--boqtv30cc" -->

---
title: #3 Introduction to solidity smart contracts storage...
description: Solidity stores data in 32-byte storage slots, packing variables efficiently based on data type and declaration order. Dynamic arrays and strings store only...
canonical: https://daily.dev/posts/3-introduction-to-solidity-smart-contracts-storage-layout----what-are-risks-in-manipulating-storage-boqtv30cc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: #3 Introduction to solidity smart contracts storage layout -- What are risks in manipulating storage??? | daily.dev
og:description: Solidity stores data in 32-byte storage slots, packing variables efficiently based on data type and declaration order. Dynamic arrays and strings store only...
og:url: https://daily.dev/posts/3-introduction-to-solidity-smart-contracts-storage-layout----what-are-risks-in-manipulating-storage-boqtv30cc
og:image: https://api.daily.dev/og/posts/boqtv30CC.png
og:image:alt: #3 Introduction to solidity smart contracts storage layout -- What are risks in manipulating storage???
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# #3 Introduction to solidity smart contracts storage layout -- What are risks in manipulating storage???

**[Ethereum BlockChain Development](https://daily.dev/sources/anuragiiitt)** · [@abiencodepacked](https://daily.dev/abiencodepacked) · 3 min read · 4 upvotes · 0 comments

## Summary

Solidity stores data in 32-byte storage slots, packing variables efficiently based on data type and declaration order. Dynamic arrays and strings store only pointers in their assigned slots, with actual data stored at computed hash locations. Array underflow vulnerabilities can allow attackers to manipulate arbitrary storage slots by causing array length to wrap around to maximum value, potentially overwriting critical variables like contract owner addresses. Mitigation includes using Solidity 0.8.0+, avoiding public write access to dynamic arrays, and implementing proper bounds checking.

## Content

# **Storage layout and array overflow/underflow risks!!!!**

- Solidity stores data in `Each slot = 32 bytes (256 bits)`
- Each storage slot is numbered: `slot 0, slot 1, slot 2` and so on.

**Solidity tries to pack variables into these slots efficiently, but the actual layout depends on data type and declaration order.**

# Storage Layout Breakdown

- Consider a simple contract with various variable and it's storage layout

```js
contract StorageExample {
    bool public flag;                    // 1 byte
    uint256 public number;        // 32 bytes
    string public message;        // dynamic
    bytes32[] public data;        // dynamic array
}
```

1. **bool public flag**

- bool = 1 byte
- Stored in `slot 0` (remaining 31 bytes unused)
- Solidity will check if next type can be packed in slot 0. If not `new slot 1` will be allocated and fitted

2. **uint256 public number**

- `uint256 = 32 bytes ` (can't be fit in slot 0)
- Stored in `slot 1` (entire slot used)

3. **string public message**

- `string is a dynamic type.` (entire slot 2 used)
- Solidity stores only a `pointer/reference` (not the data) in the slot.
- The actual string content is stored at `keccak256(2)`

```js
"hello" → 5 bytes
Stored at hash(slot 2) = location A, value: "hello"
```

4. **bytes32[] public data**

- entire slot 3 used (data.length)
- bytes32[] is a dynamic array
- `slot stores only The length of the array`

```js
// bytes32[] stored at slot 3
start of array -> uint256(keccak256(abi.encode(3)));

// Length in slot 3: 2
If data = [0x1, 0x2];

hash(slot 3) + 0 = 0x...A → 0x1
hash(slot 3) + 1 = 0x...B → 0x2
```

# Possible risks or attacks due to storage layout manipulation

```js
// We will underflow the array
contract WeirdStorage {
    address public owner;           // slot 0
    bytes32[] public data;         // slot 1 -> stores (data.length)

    function setData(uint256 index, bytes32 value) public {
        data[index] = value;
    }
}
// keccak256(1) + i = data[i];
```

**This is how attackers can corrupt unrelated storage — like overwriting a bool variable or even an owner address stored in early slots.**

1. **data.pop() -> `data.length == 2^256-1`**

2. **find index that maps to slot 0 (where owner stored)**

```js
keccak256(1) + index = 0
index = -keccak256(1) mod 2^256
index = 2^256 - keccak256(1)

vulnerable.popData(); // underflow

// attacker address, cast to bytes32
bytes32 attackerAddress = bytes32(uint256(uint160(attacker)));

// overwrite slot 0 (owner) to attacker address
vulnerable.setData(index, attackerAddress);
```

# **Mitigation**:

1. Use Solidity ≥ 0.8.0
2. Avoid `exposing publi`c write access to dynamic arrays
3. Never allow `.pop() or manual index writes from untrusted users`
4. Use `SafeMath or OpenZeppelin's defensive wrappers` for older versions

# Case Studies

- https://weka.medium.com/announcing-the-winners-of-the-first-underhanded-solidity-coding-contest-282563a87079

---

Tags: [#security](https://daily.dev/tags/security), [#blockchain](https://daily.dev/tags/blockchain), [#ethereum](https://daily.dev/tags/ethereum), [#smart-contracts](https://daily.dev/tags/smart-contracts), [#solidity](https://daily.dev/tags/solidity)

[View this post on daily.dev](https://daily.dev/posts/3-introduction-to-solidity-smart-contracts-storage-layout----what-are-risks-in-manipulating-storage-boqtv30cc)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/3-introduction-to-solidity-smart-contracts-storage-layout----what-are-risks-in-manipulating-storage-boqtv30cc","headline":"#3 Introduction to solidity smart contracts storage layout -- What are risks in manipulating storage???","text":"Solidity stores data in 32-byte storage slots, packing variables efficiently based on data type and declaration order. Dynamic arrays and strings store only pointers in their assigned slots, with actual data stored at computed hash locations. Array underflow vulnerabilities can allow attackers to manipulate arbitrary storage slots by causing array length to wrap around to maximum value, potentially overwriting critical variables like contract owner addresses. Mitigation includes using Solidity 0.8.0+, avoiding public write access to dynamic arrays, and implementing proper bounds checking.","url":"https://daily.dev/posts/3-introduction-to-solidity-smart-contracts-storage-layout----what-are-risks-in-manipulating-storage-boqtv30cc","datePublished":"2025-06-30T19:16:00.480Z","dateModified":"2025-06-30T19:16:23.864Z","author":{"@type":"Person","name":"Anurag Shingare","url":"https://daily.dev/abiencodepacked","image":"https://media.daily.dev/image/upload/s--pLWLGNO0--/f_auto/v1735198145/avatars/avatar_HWlqjvZSl79rCUSk3LxVQ","description":"Web3 and Ethereum BlockChain | Nextjs | Solidity | Hardhat | Foundry | GenAI application |Typescript","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":30}},"image":"https://media.daily.dev/image/upload/s--zY6h8z83--/f_auto/v1751310961/posts/boqtv30CC?_a=BAMClqZW0","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":4},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/anuragiiitt","name":"Ethereum BlockChain Development"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Ethereum BlockChain Development","item":"https://daily.dev/squads/anuragiiitt"},{"@type":"ListItem","position":3,"name":"#3 Introduction to solidity smart contracts storage layout -- What are risks in manipulating storage???"}]}
```

