Changeset 37352


Ignore:
Timestamp:
2023-02-24T14:17:25+13:00 (14 months ago)
Author:
davidb
Message:

STL string and c_str() combination recoded due to compiler error from MacOS gcc/clang. See comment in code for more details

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/common-src/indexers/mgpp/jni/MGPPSearchWrapperImpl.cpp

    r35552 r37352  
    589589         <<"max docs\t"<<data->queryInfo->maxDocs<<endl<<ends;
    590590
    591   const char *result = output.str().c_str();
    592   jstring j_result = j_env->NewStringUTF(result);
    593   delete (char *)result;
     591  // The following 'one-liner' is determiend by MacOS gcc/clang to be fundamentally
     592  // not safe.  This is because it will return a pointer to the cstr inside the string
     593  // returned as output.str(), but that string will be destroyed at the end of the method
     594  // meaning the 'cstr' is a dangling pointer
     595  //
     596  // In actual fact, the way we then take a copy of the cstr and put it into j_result
     597  // on this occassion means the dangling pointer isn't subsequently used
     598  //
     599  // However, let's recode to keep everything above board!
     600 
     601  //const char *result = output.str().c_str();
     602 
     603  const string result_str = output.str();
     604  char* result_cstr = new char [result_str.length()+1];
     605  strcpy (result_cstr, result_str.c_str());
     606 
     607  jstring j_result = j_env->NewStringUTF(result_cstr);
     608  delete (char *)result_cstr;
    594609  return j_result;
    595610}
Note: See TracChangeset for help on using the changeset viewer.