[asterisk-bugs] [Asterisk 0011348]: RTP session ID is negative half the time on x86_64

noreply at bugs.digium.com noreply at bugs.digium.com
Fri Nov 23 08:10:24 CST 2007


A NOTE has been added to this issue. 
====================================================================== 
http://bugs.digium.com/view.php?id=11348 
====================================================================== 
Reported By:                sperreault
Assigned To:                
====================================================================== 
Project:                    Asterisk
Issue ID:                   11348
Category:                   Channels/chan_sip/General
Reproducibility:            random
Severity:                   major
Priority:                   normal
Status:                     new
Asterisk Version:            SVN 
SVN Branch (only for SVN checkouts, not tarball releases):  trunk 
SVN Revision (number only!): 89526 
Disclaimer on File?:        N/A 
Request Review:              
====================================================================== 
Date Submitted:             11-22-2007 08:19 CST
Last Modified:              11-23-2007 08:10 CST
====================================================================== 
Summary:                    RTP session ID is negative half the time on x86_64
Description: 
I'm surprised this hasn't been reported before (which makes me doubt my
findings).

ast_random() returns a positive long. When this gets truncated to an int,
it is no longer guaranteed positive. It will be negative exactly half the
time. This may make clients very unhappy. Eyebeam, for example, drops the
connection when it sees a negative session ID.
====================================================================== 

---------------------------------------------------------------------- 
 sperreault - 11-23-07 08:10  
---------------------------------------------------------------------- 
Hmmm... let's see...

#ifdef linux
#define ast_random random
#else
long int ast_random(void);
#endif

[...]

/*! \brief glibc puts a lock inside random(3), so that the results are
thread-safe.
 * BSD libc (and others) do not. */
#ifndef linux

AST_MUTEX_DEFINE_STATIC(randomlock);

long int ast_random(void)
{
        long int res;
        ast_mutex_lock(&randomlock);
        res = random();
        ast_mutex_unlock(&randomlock);
        return res;
}
#endif

Calls to ast_random() always result in calls to random(), which always
returns a number between 0 and RAND_MAX, which is safely castable to an
int. 1.4 is safe. 

Issue History 
Date Modified   Username       Field                    Change               
====================================================================== 
11-23-07 08:10  sperreault     Note Added: 0074229                          
======================================================================




More information about the asterisk-bugs mailing list