# Kerberos context binding, segfault in test

**URL:** <https://www.ruby-forum.com/t/kerberos-context-binding-segfault-in-test/182543>\
**Category:** Ruby\
**Created:** [January 29, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/kerberos-context-binding-segfault-in-test/182543 "2010-01-29T17:03:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [January 29, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/kerberos-context-binding-segfault-in-test/182543/1 "2010-01-29T17:03:53Z")

</div>

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
