Domanda

Sto usando la seguente funzione a ciclo attraverso un paio di aperti tabelle hash CDB. A volte il valore di una data chiave viene restituita insieme ad un carattere aggiuntivo (in particolare un CTRL-P (un carattere DLE / 0x16 / 0o020)).

Ho controllato le coppie chiave CDB / valore con un paio di diverse utenze e nessuno di loro mostra altri caratteri allegate ai valori.

I ottenere il carattere se uso cdb_read () o cdb_getdata () (il codice commentata sotto).

Se dovessi indovinare direi che sto facendo qualcosa di sbagliato con il buffer creo per ottenere il risultato dalle funzioni CDB.

Qualsiasi consulenza o assistenza è molto apprezzato.

char* HashReducer::getValueFromDb(const string &id, vector <struct cdb *> &myHashFiles)
{

  unsigned char hex_value[BUFSIZ];
  size_t hex_len;

  //construct a real hex (not ascii-hex) value to use for database lookups
  atoh(id,hex_value,&hex_len);

  char *value = NULL;
  vector <struct cdb *>::iterator my_iter = myHashFiles.begin();
  vector <struct cdb *>::iterator my_end = myHashFiles.end();


  try
  {
    //while there are more databases to search and we have not found a match
    for(; my_iter != my_end && !value ; my_iter++)
    {
      //cerr << "\n looking for this MD5:" << id << " hex(" << hex_value << ") \n";
      if (cdb_find(*my_iter, hex_value, hex_len)){
          //cerr << "\n\nI found the key " << id << " and it is " << cdb_datalen(*my_iter) << " long\n\n";
          value = (char *)malloc(cdb_datalen(*my_iter));
          cdb_read(*my_iter,value,cdb_datalen(*my_iter),cdb_datapos(*my_iter));
          //value = (char *)cdb_getdata(*my_iter);
          //cerr << "\n\nThe value is:" << value << " len is:" << strlen(value)<< "\n\n";
        };

    }
  }
  catch (...){}
  return value;
}
È stato utile?

Soluzione

In primo luogo, non ho familiarità con CDB e non credo di includere sufficienti dettagli sul tuo ambiente software qui.

Ma ammesso che è come le altre librerie di database ho usato ...

I valori probabilmente non c'è bisogno di essere NUL-terminated. Ciò significa che colata a char * e la stampa non funzionerà. Si dovrebbe aggiungere uno 0 byte di te stesso.

Quindi, malloc cdb_datalen + 1 e impostare l'ultimo carattere a 0. Poi stamparlo.

Meglio ancora, uso calloc e allocherà la memoria già impostato a zero.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top