lefantome
05-11-2010, 11:22
Allo sto cercando di scrivere un carattere su un socket usando write.
alla funzione che si occupa di ciò passo l'int fc che è il file desscriptor del socket e questa struttura
typedef struct {
char type; /** tipo del messaggio */
unsigned int length; /** lunghezza in byte */
char* buffer; /** buffer messaggio */
} message_t;
Ora questa struttura é allocata così
message_t* msg;
msg = malloc(sizeof(message_t));
msg->buffer = malloc(sizeof(char)*strlen(argv[1])+1);
msg->type = MSG_CONNECT;
msg->length = strlen(argv[1])+1;
strcpy(msg->buffer,argv[1]);
Questa é la funzione incriminata:
int sendMessage(int sc, message_t *msg){
int n =0;
int a = sizeof(msg->type);
int b = sizeof(char);
printf(" SIZE: %d %d\n",a,b); /* SIZE: 1 1 */
printf("MESSAGGIO: %c \n",msg->type); /* MESSAGGIO: C é giusto*/
/*Qui la write fallisce */
EC_SC(n += write(sc,msg->type,sizeof(char)+1), "sendMessage");
EC_SC(n += write(sc,strlen(msg->buffer)+1,sizeof(int)), "sendMessage");
EC_SC(n += write(sc,msg->buffer,strlen(msg->buffer)+1), "sendMessage");
return n;
}
Valgrind mi da questo risultato:
Syscall param write(buf) points to unaddressable byte(s)
==3182== at 0x4047EB3: __write_nocancel (syscall-template.S:82)
==3182== by 0x8048AB7: main (in /home/giulio/SOProgetto/3MSG/src/msgcli)
==3182== Address 0x43 is not stack'd, malloc'd or (recently) free'd
==3182==
sendMessage: Bad address
Ora io se alla write passo una stringa a caso e come dimensione strlen+1 funziona, mentre con msg->type no.
eppure msg->type (o msg->buffer) funzionano correttamente e stampando il loro contenuto é esatto
:confused:
alla funzione che si occupa di ciò passo l'int fc che è il file desscriptor del socket e questa struttura
typedef struct {
char type; /** tipo del messaggio */
unsigned int length; /** lunghezza in byte */
char* buffer; /** buffer messaggio */
} message_t;
Ora questa struttura é allocata così
message_t* msg;
msg = malloc(sizeof(message_t));
msg->buffer = malloc(sizeof(char)*strlen(argv[1])+1);
msg->type = MSG_CONNECT;
msg->length = strlen(argv[1])+1;
strcpy(msg->buffer,argv[1]);
Questa é la funzione incriminata:
int sendMessage(int sc, message_t *msg){
int n =0;
int a = sizeof(msg->type);
int b = sizeof(char);
printf(" SIZE: %d %d\n",a,b); /* SIZE: 1 1 */
printf("MESSAGGIO: %c \n",msg->type); /* MESSAGGIO: C é giusto*/
/*Qui la write fallisce */
EC_SC(n += write(sc,msg->type,sizeof(char)+1), "sendMessage");
EC_SC(n += write(sc,strlen(msg->buffer)+1,sizeof(int)), "sendMessage");
EC_SC(n += write(sc,msg->buffer,strlen(msg->buffer)+1), "sendMessage");
return n;
}
Valgrind mi da questo risultato:
Syscall param write(buf) points to unaddressable byte(s)
==3182== at 0x4047EB3: __write_nocancel (syscall-template.S:82)
==3182== by 0x8048AB7: main (in /home/giulio/SOProgetto/3MSG/src/msgcli)
==3182== Address 0x43 is not stack'd, malloc'd or (recently) free'd
==3182==
sendMessage: Bad address
Ora io se alla write passo una stringa a caso e come dimensione strlen+1 funziona, mentre con msg->type no.
eppure msg->type (o msg->buffer) funzionano correttamente e stampando il loro contenuto é esatto
:confused: