changed basicShift and basicFill primitives not to validate source position and destination position when count is <= 0

This commit is contained in:
hyunghwan.chung 2018-01-04 10:07:42 +00:00
parent f0df2fcd92
commit 506c6900e0
3 changed files with 64 additions and 33 deletions

View File

@ -104,11 +104,17 @@ class(#pointer) Array(Collection)
}.
}
method copyFrom: start
{
| newsz |
newsz := (self size) - start.
^(self class new: newsz) copy: self from: start to: ((self size) - 1).
}
method copyFrom: start to: end
{
## returns a copy of the receiver starting from the element
## at index 'start' to the element at index 'end'.
| newsz |
newsz := end - start + 1.
^(self class new: newsz) copy: self from: start to: end

View File

@ -25,7 +25,7 @@ class(#byte) IP4Address(IPAddress)
^self new fromString: str.
}
method __fromString: str
method __fromString: str offset: offset
{
| dots digits pos size c acc |
@ -41,7 +41,7 @@ class(#byte) IP4Address(IPAddress)
if (pos >= size)
{
if (dots < 3 or: [digits == 0]) { ^Error.Code.EINVAL }.
self basicAt: dots put: acc.
self basicAt: (dots + offset) put: acc.
break.
}.
@ -57,7 +57,7 @@ class(#byte) IP4Address(IPAddress)
elsif (c = $.)
{
if (dots >= 3 or: [digits == 0]) { ^Error.Code.EINVAL }.
self basicAt: dots put: acc.
self basicAt: (dots + offset) put: acc.
dots := dots + 1.
acc := 0.
digits := 0.
@ -79,24 +79,24 @@ class(#byte) IP4Address(IPAddress)
method fromString: str
{
if ((self __fromString: str) isError)
if ((self __fromString: str offset: 0) isError)
{
Exception signal: ('invalid IPv4 address ' & str).
}
}
}
class(#byte) IP6Address(IPAddress)
class(#byte) IP6Address(IP4Address)
{
method(#class) new
{
^self basicNew: 16.
}
method(#class) fromString: str
{
^self new fromString: str.
}
##method(#class) fromString: str
##{
## ^self new fromString: str.
##}
method __fromString: str
{
@ -164,7 +164,7 @@ class(#byte) IP6Address(IPAddress)
if (ch == $. and: [tgpos + 4 <= mysize])
{
IP4Address __fromString: (str copyFrom: curseg).
if ((super __fromString: (str copyFrom: curseg) offset: tgpos) isError) { ^Error.Code.EINVAL }.
tgpos := tgpos + 4.
saw_xdigit := false.
break.
@ -185,6 +185,9 @@ class(#byte) IP6Address(IPAddress)
if (colonpos >= 0)
{
## double colon position
tgpos dump.
colonpos dump.
'--------' dump.
self basicShiftFrom: colonpos to: (colonpos + (mysize - tgpos)) count: (tgpos - colonpos).
##tgpos := tgpos + (mysize - tgpos).
}
@ -378,6 +381,13 @@ s := IP4Address fromString: '192.168.123.232'.
s dump.
s basicSize dump.
##s := IP6Address fromString: 'fe80::c225:e9ff:fe47:99.2.3.4'.
##s := IP6Address fromString: '::99.12.34.54'.
s := IP6Address fromString: '::FFFF:0:0'.
s := IP6Address fromString: 'fe80::'.
s dump.
s basicSize dump.
s := IP6Address fromString: 'fe80::c225:e9ff:fe47:b1b6'.
s dump.
s basicSize dump.

View File

@ -465,13 +465,6 @@ moo_pfrc_t moo_pf_basic_fill (moo_t* moo, moo_ooi_t nargs)
moo_seterrbfmt (moo, MOO_EINVAL, "invalid source position - %O", spos);
return MOO_PF_FAILURE;
}
if (sidx >= MOO_OBJ_GET_SIZE(rcv))
{
/* index out of range */
moo_seterrbfmt (moo, MOO_ERANGE, "source position out of bound - %zu", sidx);
return MOO_PF_FAILURE;
}
if (moo_inttooow(moo, slen, &ssz) <= 0)
{
/* negative integer or not integer */
@ -479,6 +472,20 @@ moo_pfrc_t moo_pf_basic_fill (moo_t* moo, moo_ooi_t nargs)
return MOO_PF_FAILURE;
}
if (ssz <= 0)
{
/* no filling is performed. also no validation about the range of
* source position is performed */
goto done;
}
if (sidx >= MOO_OBJ_GET_SIZE(rcv))
{
/* index out of range */
moo_seterrbfmt (moo, MOO_ERANGE, "source position out of bound - %zu", sidx);
return MOO_PF_FAILURE;
}
maxlen = MOO_OBJ_GET_SIZE(rcv) - sidx;
if (ssz > maxlen) ssz = maxlen;
end = sidx + ssz;
@ -519,6 +526,7 @@ moo_pfrc_t moo_pf_basic_fill (moo_t* moo, moo_ooi_t nargs)
}
done:
#if defined(MOO_LIMIT_OBJ_SIZE)
MOO_ASSERT (moo, ssz <= MOO_SMOOI_MAX);
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ssz));
@ -574,19 +582,32 @@ moo_pfrc_t moo_pf_basic_shift (moo_t* moo, moo_ooi_t nargs)
moo_seterrbfmt (moo, MOO_EINVAL, "invalid source position - %O", spos);
return MOO_PF_FAILURE;
}
if (sidx >= MOO_OBJ_GET_SIZE(rcv))
{
/* index out of range */
moo_seterrbfmt (moo, MOO_ERANGE, "source position out of bound - %zu", sidx);
return MOO_PF_FAILURE;
}
if (moo_inttooow(moo, dpos, &didx) <= 0)
{
/* negative integer or not integer */
moo_seterrbfmt (moo, MOO_EINVAL, "invalid destination position - %O", dpos);
return MOO_PF_FAILURE;
}
if (moo_inttooow(moo, slen, &ssz) <= 0)
{
/* negative integer or not integer */
moo_seterrbfmt (moo, MOO_EINVAL, "invalid shift count - %O", slen);
return MOO_PF_FAILURE;
}
if (ssz <= 0)
{
/* no shifting is performed. also no validation about the range of
* source position and destionation position is performed */
goto done;
}
if (sidx >= MOO_OBJ_GET_SIZE(rcv))
{
/* index out of range */
moo_seterrbfmt (moo, MOO_ERANGE, "source position out of bound - %zu", sidx);
return MOO_PF_FAILURE;
}
if (didx >= MOO_OBJ_GET_SIZE(rcv))
{
/* index out of range */
@ -594,14 +615,7 @@ moo_pfrc_t moo_pf_basic_shift (moo_t* moo, moo_ooi_t nargs)
return MOO_PF_FAILURE;
}
if (moo_inttooow(moo, slen, &ssz) <= 0)
{
/* negative integer or not integer */
moo_seterrbfmt (moo, MOO_EINVAL, "invalid shift count - %O", slen);
return MOO_PF_FAILURE;
}
if (sidx != didx && ssz > 0)
if (sidx != didx)
{
moo_oow_t maxlen;
@ -699,6 +713,7 @@ moo_pfrc_t moo_pf_basic_shift (moo_t* moo, moo_ooi_t nargs)
}
}
done:
#if defined(MOO_LIMIT_OBJ_SIZE)
MOO_ASSERT (moo, ssz <= MOO_SMOOI_MAX);
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ssz));