我有此代码,它使用OCIlib:

int HoleInstrumentenDiffListeDB(GTree *tree)
{

  OCI_Connection* cn;
  OCI_Statement* st;
  OCI_Resultset* rs;
  if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
        return EXIT_FAILURE;
  char query[500];
  query[0] ='\0';
  cn = OCI_ConnectionCreate( "db", "user",  "pass", OCI_SESSION_DEFAULT);
  st = OCI_StatementCreate(cn);
  strcat(query, "SELECT...");
  OCI_ExecuteStmt(st, query);
  rs = OCI_GetResultset(st);
  int i = 1;
  int j = 0;
  char *symbolp;
  while (OCI_FetchNext(rs)){

    const char * symbolp = OCI_GetString(rs,2);
    switch ( * OCI_GetString(rs,3))
    {
      case 'N':
        insertQot(tree, symbolp, OCI_GetInt(rs, 1) );
        printf("new \n");
        break;
      case 'U':
        insertQot(tree, symbolp, OCI_GetInt(rs, 1) );
        printf("upd \n");
        break;
      case 'D':
        deleteQot(tree, symbolp);
        printf("del \n");
        break;
     }
  }
  OCI_Cleanup();
  return 1;
}


建立连接后,选择似乎有效。但是在用valgrind检查它时,我遇到了很多错误提示者。这里有几个:

==21085== Source and destination overlap in memcpy(0x742dc80, 0x742dc80, 1)
==21085==    at 0x4A24F66: _intel_fast_memcpy (mc_replace_strmem.c:894)
==21085==    by 0x562E6D7: kpufprow (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x562BFDE: kpufch0 (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x562ACB6: kpufch (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5520A2F: OCIStmtFetch2 (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x4CDAF6E: OCI_FetchData (resultset.c:506)
==21085==    by 0x4CDB405: OCI_FetchNext (resultset.c:1117)
==21085==    by 0x401B95: HoleInstrumentenDiffListeDB (unzipper_m.c:221)
==21085==    by 0x402AA9: main (unzipper_m.c:691)


==21085== Conditional jump or move depends on uninitialised value(s)
==21085==    at 0x5EC79DF: slpmloclfv (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5EC771E: slpmloc (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5EC4C44: lpmloadpkg (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5EAAA8E: lfvLoadPkg (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5EAA719: lfvSetShlMode (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5EAA518: lfvini1 (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5EAA234: lfvinit (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5ACD1C9: kpummpin (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x547EED8: kpuenvcr (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x551FCD5: OCIEnvCreate (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x4CE311B: OCI_Initialize (library.c:1140)
==21085==    by 0x401942: HoleInstrumentenDiffListeDB (unzipper_m.c:207)



==21085== Use of uninitialised value of size 8
==21085==    at 0x56DFE05: ztceadecbk (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x56DC6BC: ztceb_decblk (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x56DC3BE: ztcebf (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x56DBB8A: ztcef (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x56DBC6E: ztcedec (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x57341A3: ztvo5ed (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x5734FA5: ztvo5ver (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x54C899D: kpu8lgn (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x54C649C: kpuauthxa (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x54C5EA5: kpuauth (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x551FF1A: OCISessionBegin (in /opt/oracle/product/10.2/lib/libclntsh.so.10.1)
==21085==    by 0x4CD45EA: OCI_ConnectionLogon (connection.c:598)


我可以改善我的代码吗?如果不是这样,将其保持现状是多么安全?

最佳答案

首先,应该每个应用程序一次调用OCI_Initialize()和OCI_Cleanup(),最好从主线程调用一次。

关于您拥有的Valgrind跟踪,您可以看到它与Oracle随附的Oracle客户端共享库中的内部代码有关。使用纯OCI代码编码的应用程序将获得相同的效果。这些跟踪与建立在顶级OCI上的OCILIB代码无关。

创建一个仅调用OCI调用OCIEnvCreate()的应用程序,然后查看Valgrind跟踪。您会有类似的痕迹。

问候,

文森特

关于c - ocilib有多稳定,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22151847/

10-11 15:54