Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes in ConvertToVectorRep and CopyToVectorRep #5126

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/vec8bit.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,17 @@ static void RewriteVec8Bit(Obj vec, UInt q)

if (q1 == q)
return;
assert(q > q1);

if (q < q1) {
ErrorMayQuit("Cannot convert a vector compressed over GF(%i) to small field GF(%i)", q1, q);
}

if (((q - 1) % (q1 - 1)) != 0) {
ErrorMayQuit("Cannot convert a vector compressed over GF(%i) to GF(%i)", q1, q);
}

if (DoFilter(IsLockedRepresentationVector, vec) == True) {
ErrorMayQuit("You cannot convert a locked vector compressed over "
ErrorMayQuit("Cannot convert a locked vector compressed over "
"GF(%i) to GF(%i)",
q1, q);
}
Expand Down Expand Up @@ -563,7 +570,6 @@ static void RewriteVec8Bit(Obj vec, UInt q)
byte = 0;
i = len - 1;

assert(((q - 1) % (q1 - 1)) == 0);
mult = (q - 1) / (q1 - 1);
while (i >= 0) {
val = VAL_FFE(convtab1[gettab1[byte1 + 256 * (i % els1)]]);
Expand Down Expand Up @@ -609,7 +615,7 @@ void RewriteGF2Vec(Obj vec, UInt q)
assert(q % 2 == 0);

if (DoFilter(IsLockedRepresentationVector, vec) == True) {
ErrorMayQuit("You cannot convert a locked vector compressed over "
ErrorMayQuit("Cannot convert a locked vector compressed over "
"GF(2) to GF(%i)",
q, 0);
}
Expand Down Expand Up @@ -685,9 +691,10 @@ static void ConvVec8Bit(Obj list, UInt q)

// already in the correct representation
if (IS_VEC8BIT_REP(list)) {
if (FIELD_VEC8BIT(list) == q)
UInt q1 = FIELD_VEC8BIT(list);
if (q1 == q)
return;
else if (FIELD_VEC8BIT(list) < q) {
else if (q1 < q && ((q - 1) % (q1 - 1)) == 0) {
RewriteVec8Bit(list, q);
return;
}
Expand Down Expand Up @@ -836,14 +843,15 @@ static Obj NewVec8Bit(Obj list, UInt q)

// already in the correct representation
if (IS_VEC8BIT_REP(list)) {
if (FIELD_VEC8BIT(list) == q) {
UInt q1 = FIELD_VEC8BIT(list);
if (q1 == q) {
res = CopyVec8Bit(list, 1);
if (!IS_MUTABLE_OBJ(list))
// index 0 is for immutable vectors
SetTypeDatObj(res, TypeVec8Bit(q, 0));
return res;
}
else if (FIELD_VEC8BIT(list) < q) {
else if (q1 < q && ((q - 1) % (q1 - 1)) == 0) {
// rewriting to a larger field
res = CopyVec8Bit(list, 1);
RewriteVec8Bit(res, q);
Expand Down Expand Up @@ -1003,7 +1011,7 @@ static Obj FuncPLAIN_VEC8BIT(Obj self, Obj list)
RequireArgument(SELF_NAME, list, "must be an 8bit vector");
}
if (DoFilter(IsLockedRepresentationVector, list) == True) {
ErrorMayQuit("You cannot convert a locked vector compressed over "
ErrorMayQuit("Cannot convert a locked vector compressed over "
"GF(%i) to a plain list",
FIELD_VEC8BIT(list), 0);
}
Expand Down
16 changes: 16 additions & 0 deletions tst/testbugfix/2022-10-18-RewriteVec8Bit.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# avoid crashes when converting certain compressed vectors
# see <https://github.com/gap-system/gap/issues/5123>

#
gap> v:= [ 0 * Z(2) ];;
gap> ConvertToVectorRep( v, 4 );
4
gap> ConvertToVectorRep( v, 8 );
8

#
gap> v:= [ 0 * Z(2) ];;
gap> ConvertToVectorRep( v, 4 );
4
gap> CopyToVectorRep( v, 8 );
[ 0*Z(2) ]