Skip to content

Commit 9b1328c

Browse files
author
runrevali
committed
[[ StdMlc ]] Make chunk ranges strict (need to add error throwing)
1 parent 0e2663a commit 9b1328c

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

libscript/src/module-byte.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ extern "C" void MCByteFetchByteRangeOf(index_t p_start, index_t p_finish, MCData
9999
{
100100
uindex_t t_start, t_count;
101101
MCChunkGetExtentsOfByteChunkByRange(p_target, p_start, p_finish, t_start, t_count);
102+
103+
if (t_count == 0)
104+
return;
105+
106+
if (t_start + t_count > MCDataGetLength(p_target))
107+
return;
108+
102109
if (!MCDataCopyRange(p_target, MCRangeMake(t_start, t_count), r_output))
103110
return;
104111
}
@@ -108,6 +115,12 @@ extern "C" void MCByteStoreByteRangeOf(MCDataRef p_value, index_t p_start, index
108115
uindex_t t_start, t_count;
109116
MCChunkGetExtentsOfByteChunkByRange(x_target, p_start, p_finish, t_start, t_count);
110117

118+
if (t_count == 0)
119+
return;
120+
121+
if (t_start + t_count > MCDataGetLength(x_target))
122+
return;
123+
111124
MCAutoDataRef t_data;
112125
if (!MCDataMutableCopy(x_target, &t_data))
113126
return;
@@ -136,6 +149,13 @@ extern "C" void MCByteStoreAfterByteOf(MCDataRef p_value, index_t p_index, MCDat
136149
{
137150
uindex_t t_start, t_count;
138151
MCChunkGetExtentsOfByteChunkByExpression(x_target, p_index, t_start, t_count);
152+
153+
if (t_count == 0)
154+
return;
155+
156+
if (t_start + t_count > MCDataGetLength(x_target))
157+
return;
158+
139159
t_start += t_count;
140160

141161
MCAutoDataRef t_data;
@@ -157,6 +177,12 @@ extern "C" void MCByteStoreBeforeByteOf(MCDataRef p_value, index_t p_index, MCDa
157177
uindex_t t_start, t_count;
158178
MCChunkGetExtentsOfByteChunkByRange(x_target, p_index, p_index, t_start, t_count);
159179

180+
if (t_count == 0)
181+
return;
182+
183+
if (t_start + t_count > MCDataGetLength(x_target))
184+
return;
185+
160186
MCAutoDataRef t_data;
161187
if (!MCDataMutableCopy(x_target, &t_data))
162188
return;

libscript/src/module-char.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ extern "C" void MCCharFetchCharRangeOf(index_t p_start, index_t p_finish, MCStri
3737
{
3838
uindex_t t_start, t_count;
3939
MCChunkGetExtentsOfCodeunitChunkByRange(p_target, p_start, p_finish, t_start, t_count);
40+
41+
if (t_count == 0)
42+
return;
43+
44+
if (t_start + t_count > MCStringGetLength(p_target))
45+
return;
46+
4047
if (!MCStringCopySubstring(p_target, MCRangeMake(t_start, t_count), r_output))
4148
return;
4249
}
@@ -46,6 +53,12 @@ extern "C" void MCCharStoreCharRangeOf(MCStringRef p_value, index_t p_start, ind
4653
uindex_t t_start, t_count;
4754
MCChunkGetExtentsOfCodeunitChunkByRange(x_target, p_start, p_finish, t_start, t_count);
4855

56+
if (t_count == 0)
57+
return;
58+
59+
if (t_start + t_count > MCStringGetLength(x_target))
60+
return;
61+
4962
MCAutoStringRef t_string;
5063
if (!MCStringMutableCopy(x_target, &t_string))
5164
return;
@@ -74,6 +87,13 @@ extern "C" void MCCharStoreAfterCharOf(MCStringRef p_value, index_t p_index, MCS
7487
{
7588
uindex_t t_start, t_count;
7689
MCChunkGetExtentsOfCodeunitChunkByRange(x_target, p_index, p_index, t_start, t_count);
90+
91+
if (t_count == 0)
92+
return;
93+
94+
if (t_start + t_count > MCStringGetLength(x_target))
95+
return;
96+
7797
t_start += t_count;
7898

7999
MCAutoStringRef t_string;
@@ -95,6 +115,12 @@ extern "C" void MCCharStoreBeforeCharOf(MCStringRef p_value, index_t p_index, MC
95115
uindex_t t_start, t_count;
96116
MCChunkGetExtentsOfCodeunitChunkByRange(x_target, p_index, p_index, t_start, t_count);
97117

118+
if (t_count == 0)
119+
return;
120+
121+
if (t_start + t_count > MCStringGetLength(x_target))
122+
return;
123+
98124
MCAutoStringRef t_string;
99125
if (!MCStringMutableCopy(x_target, &t_string))
100126
return;

libscript/src/module-list.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ extern "C" void MCListFetchElementOf(index_t p_index, MCProperListRef p_target,
101101
{
102102
uindex_t t_start, t_count;
103103
MCChunkGetExtentsOfElementChunkByExpression(p_target, p_index, t_start, t_count);
104+
105+
if (t_count == 0)
106+
return;
107+
108+
if (t_start + t_count > MCProperListGetLength(p_target))
109+
return;
110+
104111
r_output = MCValueRetain(MCProperListFetchElementAtIndex(p_target, t_start));
105112
}
106113

@@ -109,6 +116,12 @@ extern "C" void MCListStoreElementOf(MCValueRef p_value, index_t p_index, MCProp
109116
uindex_t t_start, t_count;
110117
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
111118

119+
if (t_count == 0)
120+
return;
121+
122+
if (t_start + t_count > MCProperListGetLength(x_target))
123+
return;
124+
112125
MCAutoProperListRef t_mutable_list;
113126
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
114127
return;
@@ -127,6 +140,13 @@ extern "C" void MCListFetchElementRangeOf(index_t p_start, index_t p_finish, MCP
127140
{
128141
uindex_t t_start, t_count;
129142
MCChunkGetExtentsOfElementChunkByRange(p_target, p_start, p_finish, t_start, t_count);
143+
144+
if (t_count == 0)
145+
return;
146+
147+
if (t_start + t_count > MCProperListGetLength(p_target))
148+
return;
149+
130150
MCProperListCopySublist(p_target, MCRangeMake(t_start, t_count), r_output);
131151
}
132152

@@ -135,6 +155,12 @@ extern "C" void MCListStoreElementRangeOf(MCValueRef p_value, index_t p_start, i
135155
uindex_t t_start, t_count;
136156
MCChunkGetExtentsOfElementChunkByRange(x_target, p_start, p_finish, t_start, t_count);
137157

158+
if (t_count == 0)
159+
return;
160+
161+
if (t_start + t_count > MCProperListGetLength(x_target))
162+
return;
163+
138164
MCAutoProperListRef t_mutable_list;
139165
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
140166
return;
@@ -165,6 +191,12 @@ extern "C" void MCListStoreAfterElementOf(MCValueRef p_value, index_t p_index, M
165191
t_start += t_count;
166192
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
167193

194+
if (t_count == 0)
195+
return;
196+
197+
if (t_start + t_count > MCProperListGetLength(x_target))
198+
return;
199+
168200
MCAutoProperListRef t_mutable_list;
169201
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
170202
return;
@@ -183,6 +215,12 @@ extern "C" void MCListStoreBeforeElementOf(MCValueRef p_value, index_t p_index,
183215
uindex_t t_start, t_count;
184216
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
185217

218+
if (t_count == 0)
219+
return;
220+
221+
if (t_start + t_count > MCProperListGetLength(x_target))
222+
return;
223+
186224
MCAutoProperListRef t_mutable_list;
187225
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
188226
return;
@@ -203,6 +241,12 @@ extern "C" void MCListSpliceIntoElementRangeOf(MCProperListRef p_list, index_t p
203241
uindex_t t_start, t_count;
204242
MCChunkGetExtentsOfElementChunkByRange(x_target, p_start, p_finish, t_start, t_count);
205243

244+
if (t_count == 0)
245+
return;
246+
247+
if (t_start + t_count > MCProperListGetLength(x_target))
248+
return;
249+
206250
MCAutoProperListRef t_mutable_list;
207251
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
208252
return;
@@ -227,6 +271,12 @@ extern "C" void MCListSpliceBeforeElementOf(MCProperListRef p_list, index_t p_in
227271
uindex_t t_start, t_count;
228272
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
229273

274+
if (t_count == 0)
275+
return;
276+
277+
if (t_start + t_count > MCProperListGetLength(x_target))
278+
return;
279+
230280
MCAutoProperListRef t_mutable_list;
231281
if (!MCProperListMutableCopy(x_target, &t_mutable_list))
232282
return;
@@ -245,6 +295,12 @@ extern "C" void MCListSpliceAfterElementOf(MCProperListRef p_list, index_t p_ind
245295
uindex_t t_start, t_count;
246296
MCChunkGetExtentsOfElementChunkByExpression(x_target, p_index, t_start, t_count);
247297

298+
if (t_count == 0)
299+
return;
300+
301+
if (t_start + t_count > MCProperListGetLength(x_target))
302+
return;
303+
248304
t_start += t_count;
249305

250306
MCAutoProperListRef t_mutable_list;

libscript/src/module-string.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ extern "C" void MCStringExecPutStringAfter(MCStringRef p_source, MCStringRef& x_
4141
MCValueAssign(x_target, *t_string);
4242
}
4343

44+
extern "C" void MCStringExecReplace(MCStringRef p_pattern, MCStringRef p_replacement, MCStringRef& x_target)
45+
{
46+
MCAutoStringRef t_string;
47+
if (!MCStringMutableCopy(x_target, &t_string))
48+
return;
49+
50+
MCStringFindAndReplace(*t_string, p_pattern, p_replacement, kMCStringOptionCompareExact);
51+
52+
MCAutoStringRef t_new_string;
53+
if (!MCStringCopy(*t_string, &t_new_string))
54+
return;
55+
56+
MCValueAssign(x_target, *t_new_string);
57+
}
58+
4459
extern "C" void MCStringEvalConcatenateWithSpace(MCStringRef p_left, MCStringRef p_right, MCStringRef& r_output)
4560
{
4661
if (!MCStringFormat(r_output, "%@ %@", p_left, p_right))

0 commit comments

Comments
 (0)