Hi All --

Here is a snippet of XS-ish code that I've put together to show a problem
I'm having trouble with returning many items on the stack.  When CRASH is
defined, perl segfaults running the test script.  With CRASH is not defined,
perl works fine.  It seems to be some stack issue, as the crash is after the
DoCrash function is called.  I thought perhaps the CRASH codepath was as
abuse of EXTEND, but now I'm not sure after looking at the XPUSHs macro
definition (It calls EXTEND each time).

The output is especially interesting -- it prints the strings example0 ...
example126, then segfaults.  It strikes me as some sort of stack-memory
reallocation issue.  What am I doing wrong?

Oh, this is perl 5.8.3 on Redhat 9.

Thanks,
Aren

File: testcrash.pl
---
use CrashModule;
@values = CrashModule::DoCrash(250);
print "Returned " . scalar(@values) . " results\n";
print "values are: @values.\n";

File: crash.cxx
---
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include <vector>
#include <string>

#define CRASH

#ifdef __cplusplus
extern "C" {
#endif
XS(_wrap_DoCrash) {
 dXSARGS;

 int argvi = 0;

 if ((items < 1) || (items > 1)) {
  croak("Wrong # of args");
 }

 int times = (unsigned short) SvUV(ST(0));

#ifndef CRASH
 EXTEND(sp, (times - items));
#endif

 for (int j = 0; j < times; j++) {
  char s[80];

  sprintf(s, "example%d", j);
#ifdef CRASH
  if (argvi >= items) {
   EXTEND(sp, 1);
  }
#endif

  sv_setpvn(ST(argvi) = sv_newmortal(), s, strlen(s));
  ++argvi;
 }

 XSRETURN(argvi);
}

XS(boot_CrashModule) {
    dXSARGS;
 char* file = __FILE__;
    XS_VERSION_BOOTCHECK ;

    newXSproto("CrashModule::DoCrash", _wrap_DoCrash, file, "$;$");

    XSRETURN_YES;
}

#ifdef __cplusplus
}
#endif


Reply via email to