Subject: fatal: ?Bad msgno X in mail_elt, nmsgs = Y

From: Brian Redman <[email-masked]>
Date: Tue, 14 Sep 2004 21:25:15 -0400

What's the recommended way to guard against trying to access a message 
via msgno that's greater than nmsgs.  I currently check myStream->nmsgs 
but occasionally when I mail_fetchenvelope(myStream, msgno) I get the 
fatal error anyway.

I know other mail clients may be modifying the mailbox while I'm 
monitoring it readonly and I'd be happy to get an error return but as 
it stands it's fatal.  So I assume there's a way to avoid the fatal 
error.  Is there a way to lock the stream or something so it doesn't 
get updated in the course of the mail_fetchenvelope()?

Thanks.

-- 
------------------------------------------------------------------
 For information about this mailing list, and its archives, see: 
 http://www.washington.edu/imap/c-client-list.html
------------------------------------------------------------------

---

From: Mark Crispin <[email-masked]>
Date: Tue, 14 Sep 2004 21:34:36 -0700 (PDT)

On Tue, 14 Sep 2004, Brian Redman wrote:
> What's the recommended way to guard against trying to access a message via 
> msgno that's greater than nmsgs.

Keep track of all mm_exists() and mm_expunged() events.  That will 
guarantee that at all times you know the number of messages in the 
mailbox.  Never send msgno 0 or a number greater than the number of 
messages.

> I currently check myStream->nmsgs but 
> occasionally when I mail_fetchenvelope(myStream, msgno) I get the fatal error 
> anyway.

You must be doing something wrong; because if you did that you would not 
have the problem.  Between the time you checked myStream->nmsgs and called 
mail_fetchenvelope() you must have gotten a mm_expunged() event. 
mm_expunged() events happen only at well-defined points.

Are you sure that you aren't doing something like:
 	if (msgno <= myStream->nmsgs) {
 		. . .
 	  mail_ping (myStream);
 		. . .
 	  env = mail_fetchenvelope (myStream,msgno);

the point being that you called some other mail_xxxxxx() function between 
your test and calling mail_fetchenvelope().

-- Mark --

http://staff.washington.edu/mrc
Science does not emerge from voting, party politics, or public debate.
Si vis pacem, para bellum.

---

From: Brian Redman <[email-masked]>
Date: Wed, 15 Sep 2004 00:47:23 -0400

On Sep 15, 2004, at 12:34 AM, Mark Crispin wrote:

>> What's the recommended way to guard against trying to access a 
>> message via msgno that's greater than nmsgs.
>
>> I currently check myStream->nmsgs but occasionally when I 
>> mail_fetchenvelope(myStream, msgno) I get the fatal error anyway.
>
> You must be doing something wrong; because if you did that you would 
> not have the problem.  Between the time you checked myStream->nmsgs 
> and called mail_fetchenvelope() you must have gotten a mm_expunged() 
> event. mm_expunged() events happen only at well-defined points.
>
> the point being that you called some other mail_xxxxxx() function 
> between your test and calling mail_fetchenvelope().

Thanks Mark,  If this code fragment *should* be sufficient then I'll 
look around for another cause such as faulty thread locking logic in my 
code or some-such.

     if (myStream != nil) {
		if (msgno <= myStream->nmsgs) {
         	envelope = mail_fetchenvelope(myStream, msgno);
		}
     }