[Speex-dev] Error Executing sampledec in VC++

Steve Russell srussell at innernet.net
Mon Oct 17 19:54:35 PDT 2005


Mon,

Since the buffer check was mentioned, I will pass on the bulk of another post which has comments interwoven with your sample.  This person admitted to not knowing Speex, but at least points out your buffer overflow possibility and maybe something else helpful.

Steve

>    // Program starts here:
>    cout << "Starting spxdec...\n";
>    fo = fopen("samp.spx","rb");
>
>    if (fo == NULL)
>        cout << "Error!\n";

if its not open why continue?

>    else
>        cout << "Okay!\n";
>
>    fs = fopen ("pcmfile", "wb");
>
>    if (fs == NULL)
>        cout << "Error creating file!\n";

again, why continue?

>    else
>        cout << "File created!\n";
>
>
>    while (!(feof(fo)))
>    {
>    decstate = speex_decoder_init (&speex_nb_mode);
maybe the decoder init doesn't like getting called multiple times?

>
>    // Set default options for decoding:
>    temp = 1;
>    speex_decoder_ctl(decstate, SPEEX_SET_ENH, &temp);
maybe the decoder ctl doesn't like getting called multiple times?

>
>
>    // Initialize spxbits (structure SpeexBits)
>    speex_bits_init (&spxbits);
maybe the bits init doesn't like getting called multiple times?

>
>
>        fread (&nbBytes, sizeof(int), 1, fo);
>        fread (cbits, 1, nbBytes, fo);
nbBytes could be >200 thus overflowing the buffer... all buffer sizes should 
be checked before being used.

>
>        cout << "1"; // just to see whether the loop iterates
>
>        speex_bits_read_from (&spxbits, cbits, nbBytes);
>        speex_decode (decstate, &spxbits, pcm);
>
>
>        // Copy 1 frame from float pcm to short spx
>        for (n=0; n<FRAME_SIZE; n++)
>            spx [n] = pcm [n];
>
>        fwrite (spx, sizeof(short), FRAME_SIZE, fs);
>
>    }
>     // end of  loop
>    // Entire file has been read, decoded and saved
>
>    speex_decoder_destroy (decstate);
>    speex_bits_destroy (&spxbits);
>    fclose (fo);
>    fclose (fs);
>
>    cout << "Finished processing!\n";
> }
-----------
  ----- Original Message ----- 
  From: Duane Storey 
  To: 'Mo Win' ; speex-dev at xiph.org 
  Sent: Monday, October 17, 2005 10:40 PM
  Subject: RE: [Speex-dev] Error Executing sampledec in VC++


  You should really do a check to make sure         

   

  nbBytes isn't larger than 200, otherwise you're going to read past the end of your char array.

   

  fread (&nbBytes, sizeof(int), 1, fo);

   


------------------------------------------------------------------------------

  From: speex-dev-bounces at xiph.org [mailto:speex-dev-bounces at xiph.org] On Behalf Of Mo Win
  Sent: Monday, October 17, 2005 7:31 PM
  To: speex-dev at xiph.org
  Subject: Re: [Speex-dev] Error Executing sampledec in VC++

   

  Hi Steve, thanks for the advice. I placed the appropriate functions outside the while statement but I still get this error at the while loop:

  First-chance exception at 0x004010fa in SPXDEC.exe: 0xC0000005: Access violation reading location 0x0000000c.
  Unhandled exception at 0x004010fa in SPXDEC.exe: 0xC0000005: Access violation reading location 0x0000000c.

  It looks like the same error. I've attached the code below

  Thanks,
  Mon

  Portion of the revised code:


      decstate = speex_decoder_init (&speex_nb_mode);
      // Set default options for decoding:
      temp = 1;
      speex_decoder_ctl(decstate, SPEEX_SET_ENH, &temp);

      // Initialize spxbits (structure SpeexBits)
      speex_bits_init (&spxbits);

      while (!(feof(fo)))     // this is where the problem starts
      {
          fread (&nbBytes, sizeof(int), 1, fo);
          fread (cbits, 1, nbBytes, fo);

          cout << "1"; // just to see whether the loop iterates

          speex_bits_read_from (&spxbits, cbits, nbBytes);
          speex_decode (decstate, &spxbits, pcm);


          // Copy 1 frame from float pcm to short spx
          for (n=0; n<FRAME_SIZE; n++)
              spx [n] = pcm [n];

          fwrite (spx, sizeof(short), FRAME_SIZE, fs);

      }
       // end of  loop
      // Entire file has been read, decoded and saved

      speex_decoder_destroy (decstate);
      speex_bits_destroy (&spxbits);
      fclose (fo);
      fclose (fs);
     
      cout << "Finished processing!\n";
  }

  On 10/18/05, Steve Russell <srussell at innernet.net> wrote:

  Mon,

   

  Here is feedback that I got concerning the access violation, i.e. the failure of the while loop below.  Does this solve the problem?

   

  Steve

   

  My guess is that speex_decoder_init() should be outside the while().. loop
  as speex_decoder_destroy() is also outside.

    ----- Original Message ----- 

    From: Mo Win 

    To: speex-dev at xiph.org 

    Sent: Monday, October 17, 2005 8:05 AM

    Subject: [Speex-dev] Error Executing sampledec in VC++

     

    hey guys, I just compiled an application similar to sampledec.c (for speex 1.1.10) and it was fine but when I executed it, the app exited without doing anything. I'm using MS VC 6.0 and this was all I got - First-chance exception in sampledec.exe : 0xC0000005: Access Violation. Has anyone encountered this / does anyone know how to deal with it? by the way, sampleenc executed perfectly... 

    When I removed the "while" loop in sampledec, the program executed fine (I placed printf's to check which lines were executed). I also tried running the loop only once (erasing only the while statement and retaining the code within it) and it worked fine again. Something seems to be happening during loop iteration that causes the program to terminate abnormally.. any clues? thanks very much for any tip..

    In gratitude,
    Mon

    (Below is my code. it's almost exactly like sampleenc except I read a file stream instead of stdin)

    #include "speex/speex.h"

    #include <stdio.h>
    #include <iostream.h>

    void main ()
    {
        // Definitions
        #define FRAME_SIZE 160
        #define FIXED_POINT

        // Variable Declarations
        FILE *fo, *fs;
        short spx [FRAME_SIZE];
        float pcm [FRAME_SIZE];
        char cbits [200];
        int nbBytes, n, temp;

        void *decstate;
        SpeexBits spxbits;


        // Program starts here:
        cout << "Starting spxdec...\n";
        fo = fopen("samp.spx","rb");

        if (fo == NULL)
            cout << "Error!\n";
        else
            cout << "Okay!\n";

        fs = fopen ("pcmfile", "wb");

        if (fs == NULL)
            cout << "Error creating file!\n";
        else
            cout << "File created!\n";


        while (!(feof(fo)))
        {
        decstate = speex_decoder_init (&speex_nb_mode);

        // Set default options for decoding:
        temp = 1;
        speex_decoder_ctl(decstate, SPEEX_SET_ENH, &temp);


        // Initialize spxbits (structure SpeexBits)
        speex_bits_init (&spxbits);


            fread (&nbBytes, sizeof(int), 1, fo);
            fread (cbits, 1, nbBytes, fo);

            cout << "1"; // just to see whether the loop iterates

            speex_bits_read_from (&spxbits, cbits, nbBytes);
            speex_decode (decstate, &spxbits, pcm);


            // Copy 1 frame from float pcm to short spx
            for (n=0; n<FRAME_SIZE; n++)
                spx [n] = pcm [n];

            fwrite (spx, sizeof(short), FRAME_SIZE, fs);

        }
         // end of  loop
        // Entire file has been read, decoded and saved

        speex_decoder_destroy (decstate);
        speex_bits_destroy (&spxbits);
        fclose (fo);
        fclose (fs);
        
        cout << "Finished processing!\n";
    }





----------------------------------------------------------------------------

    _______________________________________________
    Speex-dev mailing list
    Speex-dev at xiph.org
    http://lists.xiph.org/mailman/listinfo/speex-dev

   



------------------------------------------------------------------------------


  _______________________________________________
  Speex-dev mailing list
  Speex-dev at xiph.org
  http://lists.xiph.org/mailman/listinfo/speex-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.xiph.org/pipermail/speex-dev/attachments/20051017/8932a35d/attachment-0001.html


More information about the Speex-dev mailing list