Enno Shioji
Enno Shioji

Reputation: 26882

What type of input check can be performed against binary data in C++?

let's say I have a function like this in C++, which I wish to publish to third parties. I want to make it so that the user will know what happened, should he/she feeds invalid data in and the library crashes.

Let's say that, if it helps, I can change the interface as well.

int doStuff(unsigned char *in_someData, int in_data_length);

Apart from application specific input validation (e.g. see if the binary begins with a known identifier etc.), what can be done? E.g. can I let the user know, if he/she passes in in_someData that has only 1 byte of data but passes in 512 as in_data_length?

Note: I already asked a similar question here, but let me ask from another angle..

Upvotes: 1

Views: 321

Answers (3)

Charlie Martin
Charlie Martin

Reputation: 112366

In C++, the magic word you're looking for is "exception". That gives you a method to tell the caller something went wrong. You'll end up with code something like

int 
doStuff(unsigned char * inSomeData, int inDataLength) throws Exception {
    // do a test
    if(inDataLength == 0)
       throw new Exception("Length can't be 0");
    // only gets here if it passed the test
    // do other good stuff
    return theResult;
}

Now, there's another problem with your specific example, because there's no universal way in C or C++ to tell how long an array of primitives really is. It's all just bits, with inSomeData being the address of the first bits. Strings are a special case, because there's a general convention that a zero byte ends a string, but you can't depend on that for binary data -- a zero byte is just a zero byte.

Update

This has currently picked up some downvotes, apparently by people misled by the comment that exception specifications had been deprecated. As I noted in a comment below, this isn't actually true -- while the specification will be deprecated in C++11, it's still part of the language now, so unless questioner is a time traveler writing in 2014, the throws clause is still the correct way to write it in C++.

Also note that the original questioner says "I want to make it so that the user will know what happened, should he/she feeds [sic] invalid data in and the library crashes." Thus the question is not just what can I do to validate the input data (answer: not much unless you know more about the inputs than was stated), but then how do I tell the caller they screwed up? And the answer to that is "use the exception mechanism" which has certainly not been deprecated.

Upvotes: -2

Stas
Stas

Reputation: 11761

If you would know how many bytes passed by in_someData why would you need in_data_length at all?

Actually, you can only check in_someData for NULL and in_data_length for positive value. Then return some error code if needed. If a user passed some garbage to your function, this problem is obviously not yours.

Upvotes: 1

Flinsch
Flinsch

Reputation: 4341

It cannot be checked whether the parameter in_data_length passed to the function has the correct value. If this were possible, the parameter would be redundant and thus needless.

But a vector from the standard template library solves this:

int doStuff(const std::vector<unsigned char>& in_someData);

So, there is no possibility of a "NULL buffer" or an invalid data length parameter.

Upvotes: 4

Related Questions