#6464 closed defect (fixed)

Java method bindings with GIntBig as input argument do not work

Reported by: schau Owned by: warmerdam
Priority: normal Milestone: 2.0.3
Component: JavaBindings Version:
Severity: normal Keywords:
Cc: schau@…

Description

For Java binding methods such as Layer.GetFeature?(long fid) and Layer.DeleteFeature?(long fid) are either throwing a null pointer exception (if fid = 0) or crashes (if fid > 0).

The problem is related to typedef of GIntBig, in the generated JNI code:

JNIEXPORT jlong JNICALL Java_org_gdal_ogr_ogrJNI_Layer_1GetFeature(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2) {
  jlong jresult = 0 ;
  OGRLayerShadow *arg1 = (OGRLayerShadow *) 0 ;
  GIntBig arg2 ;
  OGRFeatureShadow *result = 0 ;
  GIntBig *argp2 ;

  (void)jenv;
  (void)jcls;
  arg1 = *(OGRLayerShadow **)&jarg1;
  argp2 = *(GIntBig **)&jarg2;     <==== here's the casting
  if (!argp2) {
    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "Attempt to dereference null GIntBig");    '''<====== I get this when I pass in 0 for fid'''
    return 0;

  }

  arg2 = *argp2;              <===== I crash here when fid <> 0
  result = (OGRFeatureShadow *)OGRLayerShadow_GetFeature(arg1,arg2);
  *(OGRFeatureShadow **)&jresult = result;
  return jresult;

}

To make the Java bindings work, I had to add the following line to swig/include/java/typemaps_java.i:

%typemap(in) (GIntBig) "$1 = $input;"

and this results in the following generated code:

SWIGEXPORT jlong JNICALL Java_org_gdal_ogr_ogrJNI_Layer_1GetFeature(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jlong jarg2) {
  jlong jresult = 0 ;
  OGRLayerShadow *arg1 = (OGRLayerShadow *) 0 ;
  GIntBig arg2 ;
  OGRFeatureShadow *result = 0 ;

  (void)jenv;
  (void)jcls;
  (void)jarg1_;
  arg1 = *(OGRLayerShadow **)&jarg1;
  arg2 = jarg2;  <====== here's the change
  result = (OGRFeatureShadow *)OGRLayerShadow_GetFeature(arg1,arg2);
  *(OGRFeatureShadow **)&jresult = result;
  return jresult;
}

Change History (1)

comment:1 Changed 19 months ago by Even Rouault

Component: defaultJavaBindings
Milestone: 2.0.3
Resolution: fixed
Status: newclosed
Version: svn-trunk

trunk r34009, r34010, branches/2.1 r34011, branches/2.0 r34012 "Java bindings: fix typemap for input parameter of type GIntBig (fixes GetFeature?(long), DeleteFeature?(long), etc...) (patch by schau, #6464)"

Note: See TracTickets for help on using tickets.