Author Topic: yet another subtle bug: advicemanager.cpp  (Read 2113 times)

Ginga

  • Traveller
  • *
  • Posts: 17
    • View Profile
yet another subtle bug: advicemanager.cpp
« on: December 03, 2004, 03:28:35 pm »
hi,

in \"src/server/advicemanager.cpp\"

--------------------------------------------------------------
void AdviceManager::HandleAdviceResponse( MsgEntry *me)
{
    size_t advisorID = -1;

    CPrintf(CON_DEBUG,\"Advisor has responded.\\n\");

 Client* advisor = psserver->GetNetManager()->GetClient(me->clientnum);

    if (!advisor)
    {
        Error1(\"Got unknown client!\");
        return;
    }

    //Search loop that will found the right advisor nr in the advisors array and use it as an id
    for (size_t i = 0; i < advisors.Length(); i++)
    {
        if ( ( advisors.client_id == me->clientnum ) )
        {
            advisorID = i;
        }
    }

   if ( advisorID == -1 )
  {
       psserver->SendSystemInfo(me->clientnum,\"You need to be an advisor to use this command.\");
     return;
 }
--------------------------------------------------------------


well, I think that size_t is not a very well suited name for non-storage related things, but anyway it\'s the same problem as the bug reported yesterday, since size_t is unsigned integer -- the if never becomes true.

(that\'s the bug that I was talking about in the IRC with blueCommand, they thought that was me wanting to initializate that variable to -1).

jorrit

  • Developers
  • Hydlaa Citizen
  • *
  • Posts: 497
    • View Profile
(No subject)
« Reply #1 on: December 03, 2004, 04:24:00 pm »
Quote
Originally posted by Ginga
well, I think that size_t is not a very well suited name for non-storage related things, but anyway it\'s the same problem as the bug reported yesterday, since size_t is unsigned integer -- the if never becomes true.


\'size_t\' is standard C++. We cannot change C++ to change that name :-)

Greetings,
Project Manager of Crystal Space, CEL, CrystalBlend and Crystal Core. Please support Crystal Space with a donation.

jorrit

  • Developers
  • Hydlaa Citizen
  • *
  • Posts: 497
    • View Profile
(No subject)
« Reply #2 on: December 03, 2004, 04:29:08 pm »
Quote
Originally posted by Ginga
(that\'s the bug that I was talking about in the IRC with blueCommand, they thought that was me wanting to initializate that variable to -1).


That\'s not true either. At machine level there is only a bitwise comparison done. And signed -1 is bitwise equal with unsigned ~0. So that if CAN return true. It is only the compiler that (rightly) warns about this. The correct if construct would be:

if (bla == (size_t)-1)

Greetings,
Project Manager of Crystal Space, CEL, CrystalBlend and Crystal Core. Please support Crystal Space with a donation.

Ginga

  • Traveller
  • *
  • Posts: 17
    • View Profile
(No subject)
« Reply #3 on: December 03, 2004, 06:06:42 pm »
Quote
Originally posted by jorrit
Quote
Originally posted by Ginga
well, I think that size_t is not a very well suited name for non-storage related things, but anyway it\'s the same problem as the bug reported yesterday, since size_t is unsigned integer -- the if never becomes true.


\'size_t\' is standard C++. We cannot change C++ to change that name :-)

Greetings,


then you can use \"char\" for most of the unsigned int purposes, since it\'s also standard C/C++, and it\'s implemented as an 8-bit unsigned int :P  think about syntax and semantics..

Ginga

  • Traveller
  • *
  • Posts: 17
    • View Profile
(No subject)
« Reply #4 on: December 03, 2004, 06:31:14 pm »
Quote
Originally posted by jorrit
Quote
Originally posted by Ginga
(that\'s the bug that I was talking about in the IRC with blueCommand, they thought that was me wanting to initializate that variable to -1).


That\'s not true either. At machine level there is only a bitwise comparison done. And signed -1 is bitwise equal with unsigned ~0. So that if CAN return true. It is only the compiler that (rightly) warns about this. The correct if construct would be:

if (bla == (size_t)-1)

Greetings,


well, if you use a high level language to play then with bitwise values in this simple uses.. either you or I don\'t get the picture.

in fact -1 could be set in example to zero in some compiler, because it\'s the closest value in the unsigned range to -1. do you know all the possible responses for WRONG value is used for all the possible compilers that PS could be ported? what about an exception because of out of range? :)

again, it\'s not semantically correct to asign -1 to an unsigned int/size_t value, but do as you wish: you\'ll do it anyway :)

jorrit

  • Developers
  • Hydlaa Citizen
  • *
  • Posts: 497
    • View Profile
(No subject)
« Reply #5 on: December 03, 2004, 08:37:31 pm »
Quote
Originally posted by Ginga
then you can use \"char\" for most of the unsigned int purposes, since it\'s also standard C/C++, and it\'s implemented as an 8-bit unsigned int :P  think about syntax and semantics..


I don\'t understand what you mean to say here. size_t is used as the standard index type for arrays in C++ (i.e. look at STL for example). Why should we use another type for this when C++ says it should be size_t? We can hardly use char here.

Greetings,
Project Manager of Crystal Space, CEL, CrystalBlend and Crystal Core. Please support Crystal Space with a donation.

jorrit

  • Developers
  • Hydlaa Citizen
  • *
  • Posts: 497
    • View Profile
(No subject)
« Reply #6 on: December 03, 2004, 08:40:02 pm »
Quote
Originally posted by Ginga
well, if you use a high level language to play then with bitwise values in this simple uses.. either you or I don\'t get the picture.


C or C++ are not actually not THAT high-level languages. The core language features are on purpose put pretty close to the bare machine.

Quote

in fact -1 could be set in example to zero in some compiler, because it\'s the closest value in the unsigned range to -1.


Do you have a reference on this? I don\'t think this will ever happen and I doubt it is described in the C++ reference manual like this.

Quote

again, it\'s not semantically correct to asign -1 to an unsigned int/size_t value, but do as you wish: you\'ll do it anyway :)


You misunderstood me. I never said it was correct. The only thing I said was that it would work. But working != correct. Semantically it is indeed not correct and it is best that it is fixed.

Greetings,
Project Manager of Crystal Space, CEL, CrystalBlend and Crystal Core. Please support Crystal Space with a donation.

Ginga

  • Traveller
  • *
  • Posts: 17
    • View Profile
(No subject)
« Reply #7 on: December 04, 2004, 05:39:45 am »
(merging replies)

Quote
Originally posted by jorrit
C or C++ are not actually not THAT high-level languages. The core language features are on purpose put pretty close to the bare machine.


you\'re right, at least with C (I don\'t know if with C++ is on purpose or derived for relaying in C), but I think that you should only use that tricks for special cases (hardware issues, etc), not for trivial cases as this.

Quote
Quote

in fact -1 could be set in example to zero in some compiler, because it\'s the closest value in the unsigned range to -1.


Do you have a reference on this? I don\'t think this will ever happen and I doubt it is described in the C++ reference manual like this.


I don\'t, but I suppose that this is not described, and so it\'s up to each compiler to decide what to do when using wrong values. you were right: actually it works with GCC, and probably will continue to be valid in the future with this compiler, not sure about other compilers.

Quote
Quote

again, it\'s not semantically correct to asign -1 to an unsigned int/size_t value, but do as you wish: you\'ll do it anyway :)


You misunderstood me. I never said it was correct. The only thing I said was that it would work. But working != correct. Semantically it is indeed not correct and it is best that it is fixed.


I\'m glad to read this, and my apologies for the confusion. I think that any of the developers are equal or more familiar with C++ than me, and much more familiar with PlaneShift code, so it\'s up to you to decide when and how to fix it, that function is quite complex for me at the moment.

Quote
Originally posted by jorrit
Quote
Originally posted by Ginga
then you can use \"char\" for most of the unsigned int purposes, since it\'s also standard C/C++, and it\'s implemented as an 8-bit unsigned int :P  think about syntax and semantics..


I don\'t understand what you mean to say here. size_t is used as the standard index type for arrays in C++ (i.e. look at STL for example). Why should we use another type for this when C++ says it should be size_t? We can hardly use char here.


well, I missed some parts of the function, that values interact with Lenght() and the variable is used at the end of the functions as an array, so it makes sense size_t here -- my apologies again.

just to explain what I mean with the char example, I thought that size_t didn\'t make sense there, and so I mean that you can also use char as index (it works), but would be semantically incorrect as well:

[05:24] /tmp $ g++ lala.cpp && cat lala.cpp && ./a.out
#include

int main ()
{
  char lala;
  int array[21];

  for (lala = 0; lala < 21; lala++)
    {
      array[lala] = lala*lala;
      std::cout << array[lala] << \" \";
    }
  std::cout << \"\\n\";

  return 0;
}

Output: 0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400

well, so I hope that some of you pick up this and \"fix\" it some day :)


PS: is this the right place to report this bugs or not-quite-bugs? not sure about this.

Fiver

  • Wayfarer
  • *
  • Posts: 8
    • View Profile
(No subject)
« Reply #8 on: December 04, 2004, 02:30:25 pm »
why do you do it like this?

#include

int main ()

if you culd do it like this

#include

using namespace std;

int main ()

and wuldn\'t have to type the anoying  std::
?

ok i know this has nothing to do with the bug but it just botherd me and I had to ask...  :P

Androgos

  • Guest
(No subject)
« Reply #9 on: December 04, 2004, 04:02:20 pm »
Why make the whole namespace \"local\" when you only have to use 1 function twice?

Ginga

  • Traveller
  • *
  • Posts: 17
    • View Profile
(No subject)
« Reply #10 on: December 04, 2004, 04:17:37 pm »
Quote
Originally posted by Fiver
ok i know this has nothing to do with the bug but it just botherd me and I had to ask...  :P


because it\'s a quick example and your construction uses more characters than mine ;) .  Androgos said it with another words.

Fiver

  • Wayfarer
  • *
  • Posts: 8
    • View Profile
(No subject)
« Reply #11 on: December 04, 2004, 04:49:38 pm »
O... heheh, good point :P
Didn\'t think of that  :]