C++ Sybase embedded API issues against existing Solaris code on Linux

I am having problems returning a correct result_type value from ct_results from the below code snippet. Stepping through the code, app appears to be good until the call to    retcode = ct_results(m_Cmd, &result_type); . This has been running for years on Solaris but is being migrated to Linux. Numerious small casting fixups were needed to get to this point, mostly casting differences with type int and Symase CS_XXX types. To simplify, I removed much unnecessary code from this example. Can anyone assist?

Running Sybase 15 with Linux kernel 2.6.18 and g++ 4.1
-------------------------------------------------------------------------------------
bool Sybase::Process(const char *pCmd, bool bReadText)
{
   #if !defined NO_SYBASE
      char          *tmpcmd = NULL;
      CS_RETCODE     retcode;
      CS_INT         result_type;
      CS_DATAFMT     datafmt;
      int            affected, commandLength;

        if ( !pCmd )
        {
              return false;
        }

        commandLength = strlen(pCmd);

      try
      {
         CleanupData(true);
         #if !defined NO_SYBASE
            if(ct_cmd_alloc(m_Connection->Connection(), &m_Cmd) != CS_SUCCEED)
            {
             }
         #endif
      }
      catch(...)
      {
       }

      try
      {
         tmpcmd = new char [ commandLength + 1 ];
         strcpy(tmpcmd, pCmd);
         // Send database uses
         retcode = ct_command(m_Cmd, CS_LANG_CMD, tmpcmd, CS_NULLTERM, CS_UNUSED);
         delete [] tmpcmd;

         if(retcode != CS_SUCCEED)
         {
            m_Code = -1;
            return false;
         }

      }
      catch(...)
      {
         DELETE_ARRAYPTR(tmpcmd);
         return false;
      }

      try
      {
         Sem_Lock SybLock(m_Connection->m_mLockSybase);

//#define CS_SUCCEED              (CS_RETCODE)1
//#define CS_FAIL                 (CS_RETCODE)0
//#define CS_MEM_ERROR            (CS_RETCODE)-1
//#define CS_PENDING              (CS_RETCODE)-2
//#define CS_QUIET                (CS_RETCODE)-3
//#define CS_BUSY                 (CS_RETCODE)-4
//#define CS_INTERRUPT            (CS_RETCODE)-5
//#define CS_BLK_HAS_TEXT         (CS_RETCODE)-6

         retcode = ct_send(m_Cmd);
         if(retcode != CS_SUCCEED)
         {
            m_Code = -2;
            return false;
         }

      }
      catch(...)
      {
         return false;
      }

      {
         SAPI_Sem_Lock SybLock(m_Connection->m_mLockSybase);

         // @@TODO - result_type returned here is wrong
       // @@PROBLEM here, result_type not of a CS_XXX form
       // retcode = 1
       // m_Cmd = CS_COMMAND *
       // result_type = 253403074511
               retcode = ct_results(m_Cmd, &result_type);     }

      while(retcode == CS_SUCCEED)
      {
         //@@ Result type switch here drops through to default, obviously!
         switch((CS_INT)result_type)
         {
         case CS_ROW_RESULT:
            if(!Bind())
            {
               m_Code = -3;
               return false;
            }

            if(!GetRows(bReadText))
            {
               m_Code = -4;
               return false;
            }

            m_CurrentSet++;
            break;

         case CS_CMD_SUCCEED:
         case CS_CMD_DONE:
            {
            if(ct_res_info(m_Cmd, CS_ROW_COUNT, &affected, CS_UNUSED, NULL) != CS_SUCCEED) return false;
               m_vResult[(m_CurrentSet > 0 ? m_CurrentSet-1 : 0)]->m_RowsAffected = affected;
            }
            break;

         case CS_CMD_FAIL:
               m_Code = -5;
            break;

         case CS_PARAM_RESULT:
            if(!Bind())
            {
               m_Code = -6;
               return false;
            }

            if(!GetRows(bReadText))
            {
               m_Code = -7;
               return false;
            }

            m_CurrentSet++;
            break;

         case CS_STATUS_RESULT:
            if(!Bind(true))
            {
               m_Code = -8;
               return false;
            }

            if(!GetRows(bReadText))
            {
               m_Code = -10;
               return false;
            }

            m_Code = Item(m_CurrentSet, 0, "Return_Status")->GetInt();

            m_CurrentSet++;
            break;

         case CS_COMPUTE_RESULT:
            break;

         default:
            m_Code = -11;
            return false;
         };

         {
            retcode = ct_results(m_Cmd, &result_type);
         }
      }

      if(retcode == CS_END_RESULTS)
      {
      }
      else return false;
   #else
      m_Code = -9;
      return false;
   #endif
}
1240Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
the value 253,403,074,511 is far off 32-bit integer size. can you check what sizeof(CS_INT) is on your system and how CS_INT is defined?

can you also post the definitions of the valid return types?

also pCmd and commandLength after strlen would be of interest.

i also would initialize all local variables and would not delete the temporary buffer before end.

note, as ct_results returns with success, the return_type must have a valid value beside the stack memory was corrupted. from my experience with database routines, corruption could happen if you provide a buffer for an asynchronous fetch and free the buffer before the database fetch ended. as the return_type was defined on stack, it would be stack memory that was corrupted. that is likely to happen if you used or passed pointers to local variables to asynchronously running functions. then the stack memory could be reused after the local variable run out-of-scope.

Sara

Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
1240Author Commented:
Ok, thanks. Let me look into all the tips you provided and post info later.
0
1240Author Commented:
Also, this is a 64 bit compile on a 64 bit server.
0
1240Author Commented:
You Rock!!! The problem was the local variables needed initializing. The returned code is now 4046  which is CS_CMD_DONE. I am still having issues futher in the result process but this problem is resolved.

Thanks for all!!!!!!!!!!!!!!!!!1
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.