diff --git a/lib/oper1.g b/lib/oper1.g index 143fe218e51..68cc16b8070 100644 --- a/lib/oper1.g +++ b/lib/oper1.g @@ -147,6 +147,10 @@ BIND_GLOBAL( "INSTALL_METHOD_FLAGS", function( opr, info, rel, flags, baserank, method ) local methods, narg, i, k, tmp, replace, match, j, lk, rank; + if IS_IDENTICAL_OBJ(opr, method) then + Error("Cannot install an operation as a method for itself"); + fi; + if IsHPCGAP then # TODO: once the GAP compiler supports 'atomic', use that # to replace the explicit locking and unlocking here. diff --git a/tst/testbugfix/2021-03-25-InstallMethod.tst b/tst/testbugfix/2021-03-25-InstallMethod.tst new file mode 100644 index 00000000000..be4bc9da739 --- /dev/null +++ b/tst/testbugfix/2021-03-25-InstallMethod.tst @@ -0,0 +1,6 @@ +# Installing an operation as a method for itself is forbidden (now, at least; +# it used to lead to a segfault if you ever managed to trigger that "method") +# Fixes GitHub issues #1286 and #4340. +gap> foo := NewOperation("foo", [IsObject]);; +gap> InstallMethod(foo, [IsInt], foo); +Error, Cannot install an operation as a method for itself