Skip to content

Commit b665de5

Browse files
committed
refactor: extract magic numbers to constants.mbt
- Create src/constants.mbt with default_max_output_size (100MB) and default_max_input_size (1GB) - Replace all hardcoded 104857600 and 1073741824 with named constants - Update types.mbt to use constants in default() methods - Update all test files to use constants instead of magic numbers - Improves code maintainability and makes limits easier to adjust - All 125 tests pass
1 parent e7c60f0 commit b665de5

5 files changed

Lines changed: 30 additions & 23 deletions

File tree

src/constants.mbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
///|
2+
/// Default maximum output size for decompression (100MB)
3+
pub let default_max_output_size : Int = 104857600
4+
5+
///|
6+
/// Default maximum input size for decompression (1GB)
7+
pub let default_max_input_size : Int = 1073741824

src/deflate_wbtest.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ test "deflate with dictionary" {
443443
let decompressed = inflate_sync(compressed, opts={
444444
out: None,
445445
dictionary: Some(dict),
446-
max_output_size: 104857600,
447-
max_input_size: 1073741824,
446+
max_output_size: default_max_output_size,
447+
max_input_size: default_max_input_size,
448448
})
449449
assert_eq(decompressed.length(), 5)
450450
for i in 0..<5 {

src/inflate_wbtest.mbt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ test "inflt/offset/basic" {
137137
InflateState::new(2),
138138
None,
139139
None,
140-
1073741824,
141-
1073741824,
140+
default_max_input_size,
141+
default_max_input_size,
142142
dat_off=prefix,
143143
dat_end=prefix + cl,
144144
)
@@ -166,8 +166,8 @@ test "inflt/offset/with_output_buf" {
166166
InflateState::new(2),
167167
Some(out),
168168
None,
169-
1073741824,
170-
1073741824,
169+
default_max_input_size,
170+
default_max_input_size,
171171
dat_off=prefix,
172172
dat_end=prefix + cl,
173173
)
@@ -238,8 +238,8 @@ test "inflt/tuple_return/no_buf" {
238238
InflateState::new(2),
239239
None,
240240
None,
241-
1073741824,
242-
1073741824,
241+
default_max_input_size,
242+
default_max_input_size,
243243
)
244244
assert_eq(len, 300)
245245
// buf may be larger than len (over-allocated)
@@ -263,8 +263,8 @@ test "inflt/tuple_return/exact_buf" {
263263
InflateState::new(2),
264264
Some(out),
265265
None,
266-
1073741824,
267-
1073741824,
266+
default_max_input_size,
267+
default_max_input_size,
268268
)
269269
assert_eq(len, 256)
270270
assert_eq(buf.length(), 256)
@@ -287,8 +287,8 @@ test "inflt/tuple_return/oversized_buf" {
287287
InflateState::new(2),
288288
Some(out),
289289
None,
290-
1073741824,
291-
1073741824,
290+
default_max_input_size,
291+
default_max_input_size,
292292
)
293293
assert_eq(len, 100)
294294
assert_eq(buf.length(), 500)
@@ -326,7 +326,7 @@ test "inflate - custom max_output_size limit" {
326326
out: None,
327327
dictionary: None,
328328
max_output_size: 500,
329-
max_input_size: 1073741824,
329+
max_input_size: default_max_input_size,
330330
})
331331
assert_true(result is Err(_))
332332
}
@@ -344,7 +344,7 @@ test "inflate - custom max_input_size limit" {
344344
let result = try? inflate_sync(compressed, opts={
345345
out: None,
346346
dictionary: None,
347-
max_output_size: 104857600,
347+
max_output_size: default_max_output_size,
348348
max_input_size: 100,
349349
})
350350
assert_true(result is Err(_))

src/types.mbt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ pub fn InflateOptions::default() -> InflateOptions {
2323
{
2424
out: None,
2525
dictionary: None,
26-
max_output_size: 104857600,
27-
max_input_size: 1073741824,
28-
} // 100MB output, 1GB input
26+
max_output_size: default_max_output_size,
27+
max_input_size: default_max_input_size,
28+
}
2929
}
3030

3131
///|
@@ -55,8 +55,8 @@ pub fn GunzipOptions::default() -> GunzipOptions {
5555
{
5656
out: None,
5757
dictionary: None,
58-
max_output_size: 104857600,
59-
max_input_size: 1073741824,
58+
max_output_size: default_max_output_size,
59+
max_input_size: default_max_input_size,
6060
}
6161
}
6262

@@ -85,8 +85,8 @@ pub fn UnzlibOptions::default() -> UnzlibOptions {
8585
{
8686
out: None,
8787
dictionary: None,
88-
max_output_size: 104857600,
89-
max_input_size: 1073741824,
88+
max_output_size: default_max_output_size,
89+
max_input_size: default_max_input_size,
9090
}
9191
}
9292

src/zip.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ pub fn unzip_sync(
376376
InflateState::new(2),
377377
Some(out),
378378
None,
379-
1073741824,
380-
1073741824,
379+
default_max_input_size,
380+
default_max_input_size,
381381
dat_off=b_off,
382382
dat_end=b_off + sc,
383383
)

0 commit comments

Comments
 (0)