Skip to content

Commit 1fcce72

Browse files
authored
Merge pull request #2948 from pyth-network/fixing-ton-truncation
fix: handle remaining data in read_and_store_large_data when size > input
2 parents 0e6dd71 + 5e14d51 commit 1fcce72

File tree

1 file changed

+6
-25
lines changed
  • target_chains/ton/contracts/contracts/common

1 file changed

+6
-25
lines changed

target_chains/ton/contracts/contracts/common/utils.fc

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,6 @@ int keccak256_slice(slice s) inline {
3434
return keccak256_tuple(slices);
3535
}
3636

37-
;; Splits a slice into chunks of MAX_BITS bits or less, in reverse order
38-
(cell, slice) split_into_reverse_chunks(slice data, int size) {
39-
cell chunks = null();
40-
int total_bits_loaded = 0;
41-
builder current_chunk = begin_cell();
42-
while ((~ data.slice_empty?()) & (total_bits_loaded < size)) {
43-
int bits_to_load = min(min(data.slice_bits(), MAX_BITS - current_chunk.builder_bits()), size - total_bits_loaded);
44-
current_chunk = current_chunk.store_slice(data~load_bits(bits_to_load));
45-
total_bits_loaded += bits_to_load;
46-
if ((current_chunk.builder_bits() == MAX_BITS) | (size - total_bits_loaded == 0)) {
47-
slice current_chunk_slice = current_chunk.end_cell().begin_parse();
48-
if (cell_null?(chunks)) {
49-
chunks = begin_cell().store_slice(current_chunk_slice).end_cell();
50-
} else {
51-
chunks = begin_cell().store_slice(current_chunk_slice).store_ref(chunks).end_cell();
52-
}
53-
current_chunk = begin_cell();
54-
}
55-
if ((data.slice_bits() == 0) & (~ data.slice_refs_empty?())) {
56-
data = data~load_ref().begin_parse();
57-
}
58-
}
59-
return (chunks, data);
60-
}
61-
6237
{-
6338
This function reads a specified number of bits from the input slice and stores them in a cell structure,
6439
handling data that may exceed the maximum cell capacity in FunC (1023 bits).
@@ -99,6 +74,12 @@ Note:
9974
}
10075
}
10176

77+
;; Push any remaining data in current_builder after the loop
78+
if (current_builder.builder_bits() > 0) {
79+
cell current_chunk = current_builder.end_cell();
80+
chunk_list~tpush(current_chunk);
81+
}
82+
10283
;; Build forward chain: first chunk → second chunk → third chunk etc
10384
cell result = null();
10485
int chunk_count = chunk_list.tlen();

0 commit comments

Comments
 (0)