fix memory management of current mailbox name

it was a stupid idea to store the pointer to a variable we need to
dispose in a structure which has its own lifetime.
This commit is contained in:
Oswald Buddenhagen 2014-10-04 18:26:10 +02:00
parent 4f383a8074
commit 47897d2403
4 changed files with 18 additions and 18 deletions

View File

@ -87,7 +87,6 @@ typedef struct store {
/* currently open mailbox */ /* currently open mailbox */
const char *orig_name; /* foreign! maybe preset? */ const char *orig_name; /* foreign! maybe preset? */
char *name; /* foreign! maybe preset? */
char *path; /* own */ char *path; /* own */
message_t *msgs; /* own */ message_t *msgs; /* own */
int uidvalidity; int uidvalidity;
@ -168,9 +167,9 @@ struct driver {
* needed or available operations. */ * needed or available operations. */
void (*prepare_opts)( store_t *ctx, int opts ); void (*prepare_opts)( store_t *ctx, int opts );
/* Open the mailbox ctx->name. Optionally create missing boxes. /* Open the mailbox name. Optionally create missing boxes.
* As a side effect, this should resolve ctx->path if applicable. */ * As a side effect, this should resolve ctx->path if applicable. */
void (*select)( store_t *ctx, int create, void (*select)( store_t *ctx, const char *name, int create,
void (*cb)( int sts, void *aux ), void *aux ); void (*cb)( int sts, void *aux ), void *aux );
/* Load the message attributes needed to perform the requested operations. /* Load the message attributes needed to perform the requested operations.

View File

@ -88,6 +88,7 @@ typedef struct imap_store {
store_t gen; store_t gen;
const char *label; /* foreign */ const char *label; /* foreign */
const char *prefix; const char *prefix;
const char *name;
int ref_count; int ref_count;
/* trash folder's existence is not confirmed yet */ /* trash folder's existence is not confirmed yet */
enum { TrashUnknown, TrashChecking, TrashKnown } trashnc; enum { TrashUnknown, TrashChecking, TrashKnown } trashnc;
@ -1136,7 +1137,7 @@ prepare_name( char **buf, const imap_store_t *ctx, const char *prefix, const cha
static int static int
prepare_box( char **buf, const imap_store_t *ctx ) prepare_box( char **buf, const imap_store_t *ctx )
{ {
const char *name = ctx->gen.name; const char *name = ctx->name;
return prepare_name( buf, ctx, return prepare_name( buf, ctx,
(starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) ? "" : ctx->prefix, name ); (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) ? "" : ctx->prefix, name );
@ -1828,7 +1829,7 @@ imap_prepare_opts( store_t *gctx, int opts )
/******************* imap_select *******************/ /******************* imap_select *******************/
static void static void
imap_select( store_t *gctx, int create, imap_select( store_t *gctx, const char *name, int create,
void (*cb)( int sts, void *aux ), void *aux ) void (*cb)( int sts, void *aux ), void *aux )
{ {
imap_store_t *ctx = (imap_store_t *)gctx; imap_store_t *ctx = (imap_store_t *)gctx;
@ -1839,6 +1840,7 @@ imap_select( store_t *gctx, int create,
gctx->msgs = 0; gctx->msgs = 0;
ctx->msgapp = &gctx->msgs; ctx->msgapp = &gctx->msgs;
ctx->name = name;
if (prepare_box( &buf, ctx ) < 0) { if (prepare_box( &buf, ctx ) < 0) {
cb( DRV_BOX_BAD, aux ); cb( DRV_BOX_BAD, aux );
return; return;

View File

@ -924,7 +924,7 @@ maildir_app_msg( maildir_store_t *ctx, message_t ***msgapp, msg_t *entry )
} }
static void static void
maildir_select( store_t *gctx, int create, maildir_select( store_t *gctx, const char *name, int create,
void (*cb)( int sts, void *aux ), void *aux ) void (*cb)( int sts, void *aux ), void *aux )
{ {
maildir_store_t *ctx = (maildir_store_t *)gctx; maildir_store_t *ctx = (maildir_store_t *)gctx;
@ -941,15 +941,15 @@ maildir_select( store_t *gctx, int create,
#ifdef USE_DB #ifdef USE_DB
ctx->db = 0; ctx->db = 0;
#endif /* USE_DB */ #endif /* USE_DB */
if (starts_with( gctx->name, -1, "INBOX", 5 ) && (!gctx->name[5] || gctx->name[5] == '/')) { if (starts_with( name, -1, "INBOX", 5 ) && (!name[5] || name[5] == '/')) {
gctx->path = maildir_join_path( ((maildir_store_conf_t *)gctx->conf)->inbox, gctx->name + 5 ); gctx->path = maildir_join_path( ((maildir_store_conf_t *)gctx->conf)->inbox, name + 5 );
} else { } else {
if (maildir_validate_path( gctx->conf ) < 0) { if (maildir_validate_path( gctx->conf ) < 0) {
maildir_invoke_bad_callback( gctx ); maildir_invoke_bad_callback( gctx );
cb( DRV_CANCELED, aux ); cb( DRV_CANCELED, aux );
return; return;
} }
gctx->path = maildir_join_path( gctx->conf->path, gctx->name ); gctx->path = maildir_join_path( gctx->conf->path, name );
} }
if ((ret = maildir_validate( gctx->path, create, ctx )) != DRV_OK) { if ((ret = maildir_validate( gctx->path, create, ctx )) != DRV_OK) {

View File

@ -147,7 +147,7 @@ typedef struct sync_rec {
typedef struct { typedef struct {
int t[2]; int t[2];
void (*cb)( int sts, void *aux ), *aux; void (*cb)( int sts, void *aux ), *aux;
char *dname, *jname, *nname, *lname; char *dname, *jname, *nname, *lname, *box_name[2];
FILE *jfp, *nfp; FILE *jfp, *nfp;
sync_rec_t *srecs, **srecadd; sync_rec_t *srecs, **srecadd;
channel_conf_t *chan; channel_conf_t *chan;
@ -598,14 +598,13 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
svars->uidval[0] = svars->uidval[1] = -1; svars->uidval[0] = svars->uidval[1] = -1;
svars->srecadd = &svars->srecs; svars->srecadd = &svars->srecs;
ctx[0]->name = ctx[1]->name = 0;
for (t = 0; t < 2; t++) { for (t = 0; t < 2; t++) {
ctx[t]->orig_name = ctx[t]->orig_name =
(!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ? (!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ?
"INBOX" : names[t]; "INBOX" : names[t];
if (!ctx[t]->conf->flat_delim) { if (!ctx[t]->conf->flat_delim) {
ctx[t]->name = nfstrdup( ctx[t]->orig_name ); svars->box_name[t] = nfstrdup( ctx[t]->orig_name );
} else if (map_name( ctx[t]->orig_name, &ctx[t]->name, 0, "/", ctx[t]->conf->flat_delim ) < 0) { } else if (map_name( ctx[t]->orig_name, &svars->box_name[t], 0, "/", ctx[t]->conf->flat_delim ) < 0) {
error( "Error: canonical mailbox name '%s' contains flattened hierarchy delimiter\n", ctx[t]->orig_name ); error( "Error: canonical mailbox name '%s' contains flattened hierarchy delimiter\n", ctx[t]->orig_name );
svars->ret = SYNC_FAIL; svars->ret = SYNC_FAIL;
sync_bail3( svars ); sync_bail3( svars );
@ -620,7 +619,7 @@ sync_boxes( store_t *ctx[], const char *names[], channel_conf_t *chan,
sync_ref( svars ); sync_ref( svars );
for (t = 0; t < 2; t++) { for (t = 0; t < 2; t++) {
info( "Selecting %s %s...\n", str_ms[t], ctx[t]->orig_name ); info( "Selecting %s %s...\n", str_ms[t], ctx[t]->orig_name );
svars->drv[t]->select( ctx[t], (chan->ops[t] & OP_CREATE) != 0, box_selected, AUX ); svars->drv[t]->select( ctx[t], svars->box_name[t], (chan->ops[t] & OP_CREATE) != 0, box_selected, AUX );
if (check_cancel( svars )) if (check_cancel( svars ))
break; break;
} }
@ -665,11 +664,11 @@ box_selected( int sts, void *aux )
} }
nfasprintf( &svars->dname, "%s/." EXE "state", ctx[S]->path ); nfasprintf( &svars->dname, "%s/." EXE "state", ctx[S]->path );
} else { } else {
csname = clean_strdup( ctx[S]->name ); csname = clean_strdup( svars->box_name[S] );
if (chan->sync_state) if (chan->sync_state)
nfasprintf( &svars->dname, "%s%s", chan->sync_state, csname ); nfasprintf( &svars->dname, "%s%s", chan->sync_state, csname );
else { else {
cmname = clean_strdup( ctx[M]->name ); cmname = clean_strdup( svars->box_name[M] );
nfasprintf( &svars->dname, "%s:%s:%s_:%s:%s", global_conf.sync_state, nfasprintf( &svars->dname, "%s:%s:%s_:%s:%s", global_conf.sync_state,
chan->stores[M]->name, cmname, chan->stores[S]->name, csname ); chan->stores[M]->name, cmname, chan->stores[S]->name, csname );
free( cmname ); free( cmname );
@ -1919,8 +1918,8 @@ sync_bail2( sync_vars_t *svars )
static void static void
sync_bail3( sync_vars_t *svars ) sync_bail3( sync_vars_t *svars )
{ {
free( svars->ctx[M]->name ); free( svars->box_name[M] );
free( svars->ctx[S]->name ); free( svars->box_name[S] );
sync_deref( svars ); sync_deref( svars );
} }