update the out of range position test.

This commit is contained in:
anod221 2018-08-31 11:00:12 +08:00 committed by GitHub
parent f0455c25bd
commit 3b62d988d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 7 deletions

View File

@ -825,15 +825,22 @@ static int lbytearr_slice( lua_State *L )
if( end <= 0 ){
end += getLength(p);
}
if( start > end || getLength(p) <= start ){
error_handle(L, ERR_OUTOFRANGE);
lua_error(L);
return 0;
}
handle_scope_except();
Buf *r = cut(p, start, end-start);
Buf *r;
if( start > end || getLength(p) <= start ){ // empty array
r = createBuf( BYTEARRAY_RESERVE_SIZE, getNativeEndian() );
}
else{
if( start < 0 ){ // set start to 0 )
start = 0;
}
if( getLength(p) <= end ){
end = getLength(p);
}
r = cut(p, start, end-start);
}
lua_pushlightuserdata(L, r);
return 1;
}

View File

@ -294,6 +294,16 @@ local function test_slice()
for i=1, #e do
assert( e[i] == buf[i+#buffer_data-7] )
end
local f = buf:slice( -1000, -1 )
assert( #f == #buffer_data - 1 )
for i=1, #f do
assert( f[i] == buf[i] )
end
local g = buf:slice( -1000, 1000 )
assert( #g == #buffer_data )
for i=1, #g do
assert( g[i] == buf[i] )
end
end
test_readonly()