make socket_write() capable of taking ownership of the buffer

that way the user code doesn't have to free it any more.
This commit is contained in:
Oswald Buddenhagen 2011-03-13 14:12:54 +01:00
parent 8a72d204c9
commit f8d73ac346
3 changed files with 18 additions and 19 deletions

View File

@ -244,14 +244,14 @@ v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd,
else else
printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag ); printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag );
} }
if (socket_write( &ctx->conn, buf, bufl ) < 0) if (socket_write( &ctx->conn, buf, bufl, KeepOwn ) < 0)
goto bail; goto bail;
if (litplus) { if (litplus) {
if (socket_write( &ctx->conn, cmd->param.data, cmd->param.data_len ) < 0 || char *p = cmd->param.data;
socket_write( &ctx->conn, "\r\n", 2 ) < 0)
goto bail;
free( cmd->param.data );
cmd->param.data = 0; cmd->param.data = 0;
if (socket_write( &ctx->conn, p, cmd->param.data_len, GiveOwn ) < 0 ||
socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0)
goto bail;
} else if (cmd->param.cont || cmd->param.data) { } else if (cmd->param.cont || cmd->param.data) {
ctx->literal_pending = 1; ctx->literal_pending = 1;
} }
@ -777,7 +777,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
{ {
struct imap_cmd *cmdp, **pcmdp; struct imap_cmd *cmdp, **pcmdp;
char *cmd, *arg, *arg1, *p; char *cmd, *arg, *arg1, *p;
int n, resp, resp2, tag, greeted; int resp, resp2, tag, greeted;
greeted = ctx->greeting; greeted = ctx->greeting;
for (;;) { for (;;) {
@ -839,10 +839,9 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
if (cmdp->param.data) { if (cmdp->param.data) {
if (cmdp->param.to_trash) if (cmdp->param.to_trash)
ctx->trashnc = 0; /* Can't get NO [TRYCREATE] any more. */ ctx->trashnc = 0; /* Can't get NO [TRYCREATE] any more. */
n = socket_write( &ctx->conn, cmdp->param.data, cmdp->param.data_len ); p = cmdp->param.data;
free( cmdp->param.data );
cmdp->param.data = 0; cmdp->param.data = 0;
if (n < 0) if (socket_write( &ctx->conn, p, cmdp->param.data_len, GiveOwn ) < 0)
break; break;
} else if (cmdp->param.cont) { } else if (cmdp->param.cont) {
if (cmdp->param.cont( ctx, cmdp, cmd )) if (cmdp->param.cont( ctx, cmdp, cmd ))
@ -851,7 +850,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd )
error( "IMAP error: unexpected command continuation request\n" ); error( "IMAP error: unexpected command continuation request\n" );
break; break;
} }
if (socket_write( &ctx->conn, "\r\n", 2 ) < 0) if (socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0)
break; break;
if (!cmdp->param.cont) if (!cmdp->param.cont)
ctx->literal_pending = 0; ctx->literal_pending = 0;
@ -1039,18 +1038,15 @@ do_cram_auth( imap_store_t *ctx, struct imap_cmd *cmdp, const char *prompt )
{ {
imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server; imap_server_conf_t *srvc = ((imap_store_conf_t *)ctx->gen.conf)->server;
char *resp; char *resp;
int n, l; int l;
cmdp->param.cont = 0;
cram( prompt, srvc->user, srvc->pass, &resp, &l ); cram( prompt, srvc->user, srvc->pass, &resp, &l );
if (DFlags & VERBOSE) if (DFlags & VERBOSE)
printf( ">+> %s\n", resp ); printf( ">+> %s\n", resp );
n = socket_write( &ctx->conn, resp, l ); return socket_write( &ctx->conn, resp, l, GiveOwn );
free( resp );
if (n != l)
return -1;
cmdp->param.cont = 0;
return 0;
} }
#endif #endif

View File

@ -333,7 +333,8 @@ int socket_connect( const server_conf_t *conf, conn_t *sock );
int socket_start_tls( const server_conf_t *conf, conn_t *sock ); int socket_start_tls( const server_conf_t *conf, conn_t *sock );
void socket_close( conn_t *sock ); void socket_close( conn_t *sock );
int socket_read( conn_t *sock, char *buf, int len ); int socket_read( conn_t *sock, char *buf, int len );
int socket_write( conn_t *sock, char *buf, int len ); typedef enum { KeepOwn = 0, GiveOwn } ownership_t;
int socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn );
int socket_pending( conn_t *sock ); int socket_pending( conn_t *sock );
int buffer_gets( conn_t *b, char **s ); int buffer_gets( conn_t *b, char **s );

View File

@ -373,7 +373,7 @@ socket_read( conn_t *sock, char *buf, int len )
} }
int int
socket_write( conn_t *sock, char *buf, int len ) socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn )
{ {
int n; int n;
@ -383,6 +383,8 @@ socket_write( conn_t *sock, char *buf, int len )
sock->ssl ? SSL_write( sock->ssl, buf, len ) : sock->ssl ? SSL_write( sock->ssl, buf, len ) :
#endif #endif
write( sock->fd, buf, len ); write( sock->fd, buf, len );
if (takeOwn == GiveOwn)
free( buf );
if (n != len) { if (n != len) {
socket_perror( "write", sock, n ); socket_perror( "write", sock, n );
close( sock->fd ); close( sock->fd );