Calculation of signed and unsigned numbers

Source: Internet
Author: User

Today, we encountered a bug. When audioflinger returns a negative number from audiostreaminalsa: Read (), but when it should return a negative number, returns a super large positive number. After a long query, the system finally realized that the signed positive number and the unsigned positive number will cause an error during forced conversion during an expression operation. In C language, when the expression contains a signed or unsigned type, all operands are automatically converted to the unsigned type.

 

Ssize_t audiostreaminalsa: Read (void * buffer, ssize_t bytes)

{Return static_cast <ssize_t> (snd_pcm_frames_to_bytes (mhandle-> handle, n);} When n =-32, static_cast <ssize_t> (handle (mhandle-> handle, n) is a very large positive number.

Ssize_t snd_pcm_frames_to_bytes (snd_pcm_t * PCM, snd_pcm_sframes_t frames)
{
Assert (PCM );
If (check_sanity (! PCM-> setup )){
Sndmsg ("PCM not set up ");
Return-EIO;
}
Return frames * PCM-> frame_bits/8;
}

 

This is because PCM-> frame_bits is an unsigned integer. First, frames is converted to an unsigned integer. When frames =-32, it is a very large integer, frames * PCM-> frame_bits/8 is a very large integer. It is also a big integer when a signed integer is returned when the switch is performed.

ALSA-lib/src/pcmpcm_local.h
Struct _ snd_pcm

{

Unsigned int frame_bits;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.