commit 66b041882ca1b61b9e383b522a6bfcbafa262aa4 Author: Oswald Buddenhagen <o...@kde.org> Date: Sun Mar 13 14:12:54 2011 +0100
make socket_write() capable of taking ownership of the buffer that way the user code doesn't have to free it any more. src/drv_imap.c | 36 +++++++++++++++--------------------- src/isync.h | 3 ++- src/socket.c | 4 +++- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/drv_imap.c b/src/drv_imap.c index 7fbe3ac..fb6902a 100644 --- a/src/drv_imap.c +++ b/src/drv_imap.c @@ -210,7 +210,7 @@ static struct imap_cmd * v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, const char *fmt, va_list ap ) { - int n, bufl, litplus; + int bufl, litplus; const char *buffmt; char buf[1024]; @@ -253,17 +253,14 @@ v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, else printf( ">>> %d LOGIN <user> <pass>\n", cmd->tag ); } - if (socket_write( &ctx->conn, buf, bufl ) < 0) { - free( cmd->param.data ); + if (socket_write( &ctx->conn, buf, bufl, KeepOwn ) < 0) goto bail; - } if (litplus) { - n = socket_write( &ctx->conn, cmd->param.data, cmd->param.data_len ); - free( cmd->param.data ); - if (n < 0 || - socket_write( &ctx->conn, "\r\n", 2 ) < 0) - goto bail; + char *p = cmd->param.data; 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) { ctx->literal_pending = 1; } @@ -277,6 +274,7 @@ v_submit_imap_cmd( imap_store_t *ctx, struct imap_cmd *cmd, ctx->gen.bad_callback( ctx->gen.bad_callback_aux ); bail2: cmd->param.done( ctx, cmd, RESP_CANCEL ); + free( cmd->param.data ); free( cmd->cmd ); free( cmd ); deref_store( ctx ); @@ -756,7 +754,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd ) { struct imap_cmd *cmdp, **pcmdp; char *cmd, *arg, *arg1, *p; - int n, resp, resp2, tag, greeted; + int resp, resp2, tag, greeted; greeted = ctx->greeting; while (!buffer_gets( &ctx->conn, &cmd )) { @@ -816,10 +814,9 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd ) if (cmdp->param.data) { if (cmdp->param.to_trash) ctx->trashnc = 0; /* Can't get NO [TRYCREATE] any more. */ - n = socket_write( &ctx->conn, cmdp->param.data, cmdp->param.data_len ); - free( cmdp->param.data ); + p = cmdp->param.data; cmdp->param.data = 0; - if (n < 0) + if (socket_write( &ctx->conn, p, cmdp->param.data_len, GiveOwn ) < 0) break; } else if (cmdp->param.cont) { if (cmdp->param.cont( ctx, cmdp, cmd )) @@ -828,7 +825,7 @@ get_cmd_result( imap_store_t *ctx, struct imap_cmd *tcmd ) error( "IMAP error: unexpected command continuation request\n" ); break; } - if (socket_write( &ctx->conn, "\r\n", 2 ) < 0) + if (socket_write( &ctx->conn, "\r\n", 2, KeepOwn ) < 0) break; if (!cmdp->param.cont) ctx->literal_pending = 0; @@ -991,18 +988,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; char *resp; - int n, l; + int l; + + cmdp->param.cont = 0; cram( prompt, srvc->user, srvc->pass, &resp, &l ); if (DFlags & VERBOSE) printf( ">+> %s\n", resp ); - n = socket_write( &ctx->conn, resp, l ); - free( resp ); - if (n != l) - return -1; - cmdp->param.cont = 0; - return 0; + return socket_write( &ctx->conn, resp, l, GiveOwn ); } #endif diff --git a/src/isync.h b/src/isync.h index a5a3818..648d0c3 100644 --- a/src/isync.h +++ b/src/isync.h @@ -331,7 +331,8 @@ int socket_connect( 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 ); 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 buffer_gets( conn_t *b, char **s ); diff --git a/src/socket.c b/src/socket.c index 5155d25..4bb33c5 100644 --- a/src/socket.c +++ b/src/socket.c @@ -377,7 +377,7 @@ socket_read( conn_t *sock, char *buf, int len ) } int -socket_write( conn_t *sock, char *buf, int len ) +socket_write( conn_t *sock, char *buf, int len, ownership_t takeOwn ) { int n; @@ -387,6 +387,8 @@ socket_write( conn_t *sock, char *buf, int len ) sock->use_ssl ? SSL_write( sock->ssl, buf, len ) : #endif write( sock->fd, buf, len ); + if (takeOwn == GiveOwn) + free( buf ); if (n != len) { socket_perror( "write", sock, n ); close( sock->fd ); ------------------------------------------------------------------------------ Colocation vs. Managed Hosting A question and answer guide to determining the best fit for your organization - today and in the future. http://p.sf.net/sfu/internap-sfd2d _______________________________________________ isync-devel mailing list isync-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/isync-devel