J'ai un petit problème avec les fautes strcat et de segmentation. L'erreur est la suivante:Erreur de segmentation avec strcat
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00007fff82049f1f in __strcat_chk()
(gdb) where
#0 0x00007fff82049f1f in __strcat_chk()
#1 0x0000000100000adf in bloom_operation (bloom=0x100100080, item=0x100000e11 "hello world", operation=1) at bloom_filter.c:81
#2 0x0000000100000c0e in bloom_insert (bloom=0x100100080, to_insert=0x100000e11 "hello world") at bloom_filter.c:99
#3 0x0000000100000ce5 in main() at test.c:6
bloom_operation est la suivante:
int bloom_operation(bloom_filter_t *bloom, const char *item, int operation)
{
int i;
for(i = 0; i < bloom->number_of_hash_salts; i++)
{
char temp[sizeof(item) + sizeof(bloom->hash_salts[i]) + 2];
strcat(temp, item);
strcat(temp, *bloom->hash_salts[i]);
switch(operation)
{
case BLOOM_INSERT:
bloom->data[hash(temp) % bloom->buckets] = 1;
break;
case BLOOM_EXISTS:
if(!bloom->data[hash(temp) % bloom->buckets]) return 0;
break;
}
}
return 1;
}
La ligne de trouble est le secondes strcat. Le bloom-> hash_salts font partie d'une structure définie comme suit:
typedef unsigned const char *hash_function_salt[33];
typedef struct {
size_t buckets;
size_t number_of_hash_salts;
int bytes_per_bucket;
unsigned char *data;
hash_function_salt *hash_salts;
} bloom_filter_t;
Et ils sont initialisés ici:
bloom_filter_t* bloom_filter_create(size_t buckets, size_t number_of_hash_salts, ...)
{
bloom_filter_t *bloom;
va_list args;
int i;
bloom = malloc(sizeof(bloom_filter_t));
if(bloom == NULL) return NULL;
// left out stuff here for brevity...
bloom->hash_salts = calloc(bloom->number_of_hash_salts, sizeof(hash_function_salt));
va_start(args, number_of_hash_salts);
for(i = 0; i < number_of_hash_salts; ++i)
bloom->hash_salts[i] = va_arg(args, hash_function_salt);
va_end(args);
// and here...
}
Et bloom_filter_create est appelé comme suit:
bloom_filter_create(100, 4, "3301cd0e145c34280951594b05a7f899", "0e7b1b108b3290906660cbcd0a3b3880", "8ad8664f1bb5d88711fd53471839d041", "7af95d27363c1b3bc8c4ccc5fcd20f32");
Je Je fais quelque chose de mal mais je suis vraiment perdu sur quoi. Merci d'avance,
Ben.
Avec la ligne memcpy, est-il possible de faire en sorte que la variable 33 (c'est-à-dire basée sur la définition dans l'en-tête) fonctionne comme sizeof (hash_function_salt)? – benofsky
@benofsky, oui. Je vais le corriger pour utiliser 'sizeof (bloom-> hash_salts [i])'. Cela fonctionne parce que 'hash_function_salt' est un tableau, pas un pointeur. –
J'ai maintenant 'memcpy (bloom-> hash_salts [i], va_arg (args, char *), sizeof (bloom-> hash_salts [i]));' et j'obtiens l'erreur: 'warning: passing argument 1 of ' __builtin___memcpy_chk 'supprime les qualificatifs du type cible de pointeur'? – benofsky