Kerberos context binding, segfault in test

Hi,

Ruby 1.8.7-p243
test-unit 2.0.6
Snow Leopard

I’m wrapping a Kerberos context object, but I’m getting a segfault.
Specifically, one test in particular (an expected error test) seems to
be causing the segfault. Here’s the source:

#include <ruby.h>
#include <errno.h>
#include <strings.h>
#include <krb5.h>

VALUE cContextError;

typedef struct {
krb5_context context;
} rcontext;

void rkrb5_context_free(rcontext* ptr){
if(!ptr)
return;

if(ptr->context)
krb5_free_context(ptr->context);

free(ptr);
}

static VALUE rcontext_allocate(VALUE klass){
rcontext* ptr = malloc(sizeof(rcontext));
return Data_Wrap_Struct(klass, 0, rkrb5_context_free, ptr);
}

/*

  • call-seq:
  • Kerberos::Context.new
  • Creates and returns a new Kerberos::Context object.
    /
    static VALUE rcontext_initialize(VALUE self){
    rcontext
    ptr;

Data_Get_Struct(self, rcontext, ptr);

errno = krb5_init_context(&ptr->context);

if(errno)
rb_raise(cContextError, “%s”, error_message(errno));

return self;
}

/*

  • call-seq:
  • Releases the context. Returns true when closed the first time,
    false if
  • the object has already been closed.
    /
    static VALUE rcontext_close(VALUE self){
    rcontext
    ptr;
    VALUE v_bool = Qfalse;

Data_Get_Struct(self, rcontext, ptr);

if(ptr && ptr->context){
krb5_free_context(ptr->context);
v_bool = Qtrue;
}

ptr->context = NULL;

return v_bool;
}

void Init_context(){
/* The Kerberos module serves as a namespace only. */
VALUE mKerberos = rb_define_module(“Kerberos”);

/* The Context class encapsulates a Kerberos context. */
VALUE cContext = rb_define_class_under(mKerberos, “Context”,
rb_cObject);

/* The Context::Error class is raised if any of the Context methods
fail. */
cContextError = rb_define_class_under(cContext, “Error”,
rb_eStandardError);

rb_define_alloc_func(cContext, rcontext_allocate);

rb_define_method(cContext, “initialize”, rcontext_initialize, 0);
rb_define_method(cContext, “close”, rcontext_close, 0);
}

Nothing too fancy. Here’s a short test suite I have for it. They all
pass, but it seems the last test is causing a segfault:

########################################################################

test_kerberos_context.rb

Tests for the Kerberos::Context class.

########################################################################
require ‘rubygems’
gem ‘test-unit’
require ‘test/unit’
require ‘rkerberos’

class TC_Kerberos_Context < Test::Unit::TestCase
def setup
@ctx = Kerberos::Context.new
end

test “close basic functionality” do
assert_respond_to(@ctx, :close)
assert_nothing_raised{ @ctx.close }
end

test “calling close multiple times is a no-op” do
assert_nothing_raised{ @ctx.close }
assert_nothing_raised{ @ctx.close }
assert_nothing_raised{ @ctx.close }
end

test “close returns true the first time it is called” do
assert_true(@ctx.close)
end

test “close returns false if already closed” do
assert_true(@ctx.close)
assert_false(@ctx.close)
end

test “close does not accept any arguments” do
assert_raise(ArgumentError){ @ctx.close(true) }
end

This seems to be the culprit

test “constructor does not accept arguments” do
assert_raise(ArgumentError){ Kerberos::Context.new(true) }
end

def teardown
@ctx = nil
end
end

6 tests, 10 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions,
0 notifications
100% passed
/usr/local/lib/ruby/gems/1.8/gems/test-unit-2.0.6/lib/test/unit.rb:
321: [BUG] Segmentation fault
ruby 1.8.7 (2009-12-24 patchlevel 248) [i686-darwin10.2.0]

Line 321 of unit.rb is simply “exit Test::Unit::AutoRunner.run”.

Any ideas?

Regards,

Dan