Pregunta

Estoy usando la siguiente función de bucle a través de un par de tablas hash CDB abiertas. A veces, el valor de una clave dada se devuelve junto con un carácter adicional (específicamente un CTRL-P (un carácter DLE / 0x16 / 0o020)).

He comprobado los pares clave CDB / valor con un par de diferentes utilidades y ninguno de ellos mostrar los caracteres adicionales adjuntas a los valores.

me sale el personaje si uso cdb_read () o cdb_getdata () (el código comentado a continuación).

Si tuviera que adivinar, diría que estoy haciendo algo mal con el tampón se crea para obtener el resultado de las funciones del BDC.

Cualquier consejo o ayuda es muy apreciada.

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;
}
¿Fue útil?

Solución

En primer lugar, no estoy familiarizado con CDB y no creo que se incluye suficientes detalles sobre su entorno de software aquí.

Sin embargo, asumiendo que es igual que otras bibliotecas de bases de datos que he utilizado ...

Los valores probablemente no tiene que ser terminada en NUL. Eso significa que la fundición a char * e impresión no funcionará. Se debe añadir un byte 0 a sí mismo.

Así malloc cdb_datalen + 1 y se ajusta el último carácter a 0. A continuación, imprimirlo.

Mejor aún, el uso calloc y se asignará memoria ya se pone a cero.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top