Skip to content

Commit f0ecd11

Browse files
committed
FEAT: handle negative part values when reading from a file port
resolves: Oldes/Rebol-issues#2505
1 parent 3822b56 commit f0ecd11

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/core/p-file.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ REBINT Mode_Syms[] = {
370370

371371
/***********************************************************************
372372
**
373-
*/ static REBCNT Set_Length(const REBVAL *ds, const REBREQ *file, const REBCNT arg)
373+
*/ static REBCNT Set_Length(const REBVAL *ds, REBREQ *file, const REBCNT arg)
374374
/*
375375
** Computes the length of data based on the argument number
376376
** provided for the ARG_*_PART stack value (which, when there,
@@ -395,6 +395,16 @@ REBINT Mode_Syms[] = {
395395

396396
// Limit size of requested read:
397397
cnt = VAL_INT64(D_ARG(arg+1));
398+
if (cnt < 0) {
399+
cnt = -cnt;
400+
if (cnt > file->file.index) {
401+
Trap1(RE_OUT_OF_RANGE, D_ARG(arg + 1));
402+
//cnt = file->file.index;
403+
}
404+
file->file.index -= cnt;
405+
len += cnt;
406+
SET_FLAG(file->modes, RFM_RESEEK);
407+
}
398408
if (cnt > len) return (REBCNT)len;
399409
return (REBCNT)cnt;
400410
}

src/tests/units/port-test.r3

+16-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,22 @@ if system/platform = 'Windows [
331331
port? close p
332332
not error? try [delete %file-552]
333333
]
334-
334+
335+
--test-- "read/part"
336+
;@@ https://github.com/Oldes/Rebol-issues/issues/2505
337+
--assert all [
338+
file? write %12345 "12345"
339+
port? p: open/read/seek %12345
340+
#{31} == read/part p 1 ; advances!
341+
#{3233} == read/part p 2
342+
#{3132} == read/part head p 2
343+
#{} == read/part tail p 2
344+
#{35} == read/part tail p -1 ;- no crash!
345+
#{3435} == read/part tail p -2
346+
all [error? e: try [read/part p -20] e/id = 'out-of-range]
347+
port? close p
348+
not error? try [delete %12345]
349+
]
335350

336351
--test-- "CLEAR file port"
337352
;@@ https://github.com/Oldes/Rebol-issues/issues/812

0 commit comments

Comments
 (0)