Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 904497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:03:39+00:00 2026-05-15T16:03:39+00:00

In the below code, I have a corrupt hello.bz2 which has stray characters beyond

  • 0

In the below code, I have a corrupt “hello.bz2” which has stray characters beyond the EOF.

Is there a way to make the boost::iostreams::copy() call to throw ?

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>

int main() 
{
    using namespace std;
    using namespace boost::iostreams;

    ifstream file("hello.bz2", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(bzip2_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);
}

EDIT:
Please ignore the line that is so far attracted most attention; the EOF. Please assume working with a corrupted bzip2 file.
I used “EOF” suggesting the error I got when I run bzcat on the file

bzcat hello.bz2
hello world

bzcat: hello.bz2: trailing garbage after EOF ignored
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:03:40+00:00Added an answer on May 15, 2026 at 4:03 pm

    Research

    std::ios_base::failure is the “the base class for the types of all objects thrown as exceptions, by functions in the Iostreams library, to report errors detected during stream buffer operations.”

    Looking at the boost docs:

    class bzip2_error : public std::ios_base::failure {
    public:
        bzip2_error(int error);
        int error() const;
    };
    

    bzip2_error is a specific exception thrown when using the bzip2 filter, which inherits from std::ios_base::failure. As you can see, it is constructed by passing in an integer representing the error code. It also has a method error() which returns the error code it was constructed with.
    The docs list bzip2 error codes as the following:

    • data_error – Indicates that the compressed data stream is corrupted. Equal to BZ_DATA_ERROR.
    • data_error_magic – Indicates that the compressed data stream does not begin with the ‘magic’ sequence ‘B’ ‘Z’ ‘h’. Equal to BZ_DATA_ERROR_MAGIC.
    • config_error – Indicates that libbzip2 has been improperly configured for the current platform. Equal to BZ_CONFIG_ERROR.

    Code

    EDIT
    I also want to clarify that boost::iostreams::copy() will not be the one throwing the exception here, but the bzip2 filter. Only the iostream or filters will throw exceptions, copy just uses the iostream/filter which may cause the iostream/filter to throw an exception.

    **EDIT 2 **
    It appears the problem is with bzip2_decompressor_impl as you have expected. I have replicated the endless spinning loop when the bz2 file is empty. It took me a little while to figure out how to build boost and link with bzip2, zlib, and iostreams library to see if I could replicate your results.

    g++ test.cpp -lz -lbz2 boostinstall/boost/bin.v2/libs/iostreams/build/darwin-4.2.1/release/link-static/threading-multi/libboost_iostreams.a -Lboostinstall/boost/bin.v2/libs/ -Iboost/include/boost-1_42 -g
    

    test.cpp:

    #include <fstream>
    #include <iostream>
    #include <boost/iostreams/filtering_streambuf.hpp>
    #include <boost/iostreams/copy.hpp>
    #include <boost/iostreams/filter/bzip2.hpp>
    
    int main()
    {
        using namespace std;
        using namespace boost::iostreams;
    
        try {
            ifstream file("hello.bz2", ios_base::in | ios_base::binary);
            filtering_streambuf<input> in;
            in.push(bzip2_decompressor());
            in.push(file);
            boost::iostreams::copy(in, cout);
        }
        catch(const bzip2_error& exception) {
            int error = exception.error();
    
            if(error == boost::iostreams::bzip2::data_error) {
                // compressed data stream is corrupted
                cout << "compressed data stream is corrupted";
            }
            else if(error == boost::iostreams::bzip2::data_error_magic)
            {
                // compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'
                cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
            }
            else if(boost::iostreams::bzip2::config_error) {
                // libbzip2 has been improperly configured for the current platform
                cout << "libbzip2 has been improperly configured for the current platform";
            }
        }
    }
    

    debugging:

    gdb a.out
    (gdb) b bzip2.hpp:344
    

    There is a loop that drives the bzip2’s uncompression in symmetric.hpp:109 :

            while (true)
            {
                // Invoke filter if there are unconsumed characters in buffer or if
                // filter must be flushed.
                bool flush = status == f_eof;
                if (buf.ptr() != buf.eptr() || flush) {
                    const char_type* next = buf.ptr();
                    bool done =
                        !filter().filter(next, buf.eptr(), next_s, end_s, flush);
                    buf.ptr() = buf.data() + (next - buf.data());
                    if (done)
                        return detail::check_eof(
                                   static_cast<std::streamsize>(next_s - s)
                               );
                }
    
                // If no more characters are available without blocking, or
                // if read request has been satisfied, return.
                if ( (status == f_would_block && buf.ptr() == buf.eptr()) ||
                     next_s == end_s )
                {
                    return static_cast<std::streamsize>(next_s - s);
                }
    
                // Fill buffer.
                if (status == f_good)
                    status = fill(src);
            }
    

    bzip2_decompressor_impl’s filter method bzip2.hpp:344 gets called on symmetric.hpp:117 :

    template<typename Alloc>
    bool bzip2_decompressor_impl<Alloc>::filter
        ( const char*& src_begin, const char* src_end,
          char*& dest_begin, char* dest_end, bool /* flush */ )
    {
        if (!ready())
            init();
        if (eof_)
            return false;
        before(src_begin, src_end, dest_begin, dest_end);
        int result = decompress();
        after(src_begin, dest_begin);
        bzip2_error::check BOOST_PREVENT_MACRO_SUBSTITUTION(result);
        return !(eof_ = result == bzip2::stream_end);
    }
    

    I think the problem is simple, the bzip2_decompressor_impl’s eof_ flag never gets set. Unless it’s suppose to happen in some magic way I don’t understand, it’s owned by the bzip2_decompressor_impl class and it’s only ever being set to false. So when we do this:

    cat /dev/null > hello.bz2
    

    We get a spinning loop that never ends, we don’t break when an EOF is hit. This is certainly a bug, because other programs (like vim) would have no problem opening a text file created in a similar manner. However I am able to get the filter to throw when the bz2 file is “corrupted”:

    echo "other corrupt" > hello.bz2
    ./a.out
    compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'
    

    Sometimes you have to take open source code with a grain of salt. It will be more likely that your bz2’s will be corrupted and properly throw. However, the /dev/null case is a serious bug. We should submit it to the boost dev so they can fix it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd suggest putting the setting in your app.config file, and… May 16, 2026 at 9:56 am
  • Editorial Team
    Editorial Team added an answer ((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>")); or Common Tasks and How to Do Them in… May 16, 2026 at 9:56 am
  • Editorial Team
    Editorial Team added an answer I found out to be.. protected int _getInitWorkerTime() { int… May 16, 2026 at 9:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have been having an ongoing fight with this email setup. The code below
So Im trying to make an object dynamically, please see below code from my
I have a bit of code below that creates a button on a html
I have big problem. Lets look on the code below: protected void Application_AuthenticateRequest(object sender,
I have a web based program which chooses some records from a database using
I'm working on a user control right now in which I have a path
I have the array example below that I am using to dynamically create an
[Updated]: Answer inline below question I have an inspecting program and one objective is
First_Layer I have a win32 dll written in VC++6 service pack 6. Let's call
I have an existing PDF and I can use FdFWriter to input to text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.