6 Replies - 660 Views - Last Post: 31 December 2012 - 07:12 PM Rate Topic: -----

#1 Brok9n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-April 12

C++ Template Warnings

Posted 31 December 2012 - 02:33 AM

I'd like to point out that this isn't my code, and thus my understanding isn't so great. I'm implementing it in my project, however I compile with -Wextra, which produces the following warnings ( and it's bugging the hell out of me ).

In file included from src/bsha1.cpp:3:
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = unsigned int, long unsigned int size = 80ul]’:
src/bsha1.cpp:42:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = unsigned int, long unsigned int size = 21ul]’:
src/bsha1.cpp:136:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’

src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = const std::string, long unsigned int size = 3ul]’:
src/check_revision.cpp:150:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’

In file included from src/random.cpp:6:
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = ulong, long unsigned int size = 32ul]’:
src/random.cpp:99:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’
src/array.hpp: In instantiation of ‘size_t nil::countof(type (&)[size]) [with type = const bool, long unsigned int size = 11ul]’:
src/random.cpp:109:   instantiated from here
src/array.hpp:9: warning: unused parameter ‘array’


Array.hpp
#ifndef NIL_ARRAY_HPP
#define NIL_ARRAY_HPP

#include <cstddef>

namespace nil
{
	template <typename type, std::size_t size>
		std::size_t countof(type (&array)[size])
	{
		return size;
	}
}
#endif


bsha1.cpp 34-46
void calculate_hash(unsigned * buffer)
{
        unsigned hash_buffer[80];
        unsigned hash, a, b, c, d, e, hash_buffer_offset;

        for(std::size_t i = 0; i < 0x10; i++)
                        hash_buffer[i] = buffer[i + 5];

        for(std::size_t i = 0x10; i < nil::countof(hash_buffer); i++)
        {
                        hash = hash_buffer[i - 0x10] ^ hash_buffer[i - 0x8] ^ hash_buffer[i - 0xE] ^ hash_buffer[i - 0x3];
                        hash_buffer[i] = (1 >> (0x20 - (hash & 0xff))) | (1 << (hash & 0xff));
        }



The cause appears to be when a pointer to an array is passed in, and no size which I believe the template is expecting. I've tried passing size but that just causes errors. I'd expect just passing the pointer in would cause an error, but I guess not.

Oh and believe me I'd love to not use this code, but my only other option is rewriting it, which would take a considerable amount of time.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Template Warnings

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,673
  • Joined: 25-December 09

Re: C++ Template Warnings

Posted 31 December 2012 - 06:28 AM

Why are you trying to use a template at all? I recommend getting this program to work using a standard function first, then if you require a template modify that code.

Since you are using an array in your calculate function you should be passing the size into this function. Once you pass an array into a function you loose any size information. I would suggest you investigate using a vector instead of the array, then you would let the vector worry about the size.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: C++ Template Warnings

Posted 31 December 2012 - 07:29 AM

Yes, that calculate_hash() function should take an incoming size for buffer. Right now, the code there assumes that the buffer is at least 80*sizeof(unsigned) bytes long.

As for the nil::countof() causing you grief. It's not that hard to do a global search and replace to use this macro instead:
#define CountOf(_arr) (sizeof(_arr)/sizeof(_arr[0]))



I believe that whoever put together that template function was taking the advice to avoid using macros in C++ to the hilt. It looks like that template function is constructed properly to return the number of elements of the array. At least it looks correct based on what I've seen from some Microsoft headers that also do the same thing - get the number of elements for an array.
Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,673
  • Joined: 25-December 09

Re: C++ Template Warnings

Posted 31 December 2012 - 07:47 AM

But remember for that CountOf macro to work it must be called where the array was declared, it will not work in the function for the parameter. So you should pass this "size" into the function.

Also in the following snippet you may have problems of accessing the "buffer" out of bounds.
        for(std::size_t i = 0; i < 0x10; i++)
            hash_buffer[i] = buffer[i + 5];


Why are you adding 5 to the index of buffer? Also why all the "magic numbers" (0x10 and 5)? I would strongly recommend that you use '\n' in the loop instead. Remember different operating systems use different line ending characters.


Jim
Was This Post Helpful? 2
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: C++ Template Warnings

Posted 31 December 2012 - 08:27 AM

Ah... I'm a little slow on the uptake today... bsha1 == Broken SHA-1. It looks like the code snipped above from calculate_hash() matches up with:
    Extend the sixteen 32-bit words into eighty 32-bit words:
    for i from 16 to 79
        w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1


from the SHA-1 algorithm.

The magic numbers are there because they are the same magic numbers defined in the algorithm.

That still doesn't explain the 5 as a magic number, though.

This post has been edited by Skydiver: 31 December 2012 - 08:28 AM

Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,157
  • Joined: 05-May 12

Re: C++ Template Warnings

Posted 31 December 2012 - 09:24 AM

And on further searches, it looks like Broken SHA1 is used to access Blizzard's Battle.net accouts: http://www.skullsecu...-misconceptions

Are you putting together a password cracker for Battle.Net?
Was This Post Helpful? 1
  • +
  • -

#7 Brok9n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 01-April 12

Re: C++ Template Warnings

Posted 31 December 2012 - 07:12 PM

View PostSkydiver, on 01 January 2013 - 12:29 AM, said:

Yes, that calculate_hash() function should take an incoming size for buffer. Right now, the code there assumes that the buffer is at least 80*sizeof(unsigned) bytes long.

As for the nil::countof() causing you grief. It's not that hard to do a global search and replace to use this macro instead:
#define CountOf(_arr) (sizeof(_arr)/sizeof(_arr[0]))



I believe that whoever put together that template function was taking the advice to avoid using macros in C++ to the hilt. It looks like that template function is constructed properly to return the number of elements of the array. At least it looks correct based on what I've seen from some Microsoft headers that also do the same thing - get the number of elements for an array.


Thanks for the macro that solved my warnings, and I was also confused why a template was used for something so simple.

View PostSkydiver, on 01 January 2013 - 02:24 AM, said:

And on further searches, it looks like Broken SHA1 is used to access Blizzard's Battle.net accouts: http://www.skullsecu...-misconceptions

Are you putting together a password cracker for Battle.Net?


You're right, I'm using Broken SHA1 to access Battle.net servers, however I'm not using it for malicious purposes, e.g, password cracker.

Thanks for the help, appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1