Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
Bug in will_fall( )
Oct 23, 2017, 1:35 am
By GatewaySysop
Bug in do_zap( ), do_brandish( )
Oct 18, 2017, 1:52 pm
By GatewaySysop
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Memwatch
Author: Johan Lindh
Submitted by: Vladaar
Users Online
CommonCrawl, Google, Sogou, Yahoo!, DotBot

Members: 0
Guests: 12
Stats
Files
Topics
Posts
Members
Newest Member
477
3,706
19,240
608
LAntorcha
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » Codebases » AFKMud Support & Development » Buffer overflow causing crash
Forum Rules | Mark all | Recent Posts

Buffer overflow causing crash
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Mar 5, 2010, 10:04 pm   Last edited Mar 5, 2010, 10:24 pm by Riffic
Go to the top of the page
Go to the bottom of the page

Riffic
Fledgling
GroupMembers
Posts13
JoinedApr 15, 2009

Alright, I've decided to give afkmud a try so I'm running it on Ubuntu 9.10 with the compiler and it various devs and libs upgraded. There were a few errors on a make clean, but it still started up and ran just fine. I logged in as Admin and that worked fine. When I created my first player, it went through the creation just fine until it finished setting the characters sex. It shows this on kmuddy:

No help on 'nmotd' found.
Suggested Help Files:
NMOTD Opening only suggested helpfile.

Then the connection dies and my console tells me it was from a buffer overflow. Well, here it is exactly:

*** buffer overflow detected ***: ../src/afkmud terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0x60aed8]
/lib/tls/i686/cmov/libc.so.6[0x609f10]
/lib/tls/i686/cmov/libc.so.6(__strcpy_chk+0x44)[0x609284]
../src/afkmud(_Z8show_mapP9char_dataPc+0x301)[0x81ba790]
../src/afkmud(do_look+0x44a)[0x80a6016]
../src/afkmud(_Z9interpretP9char_dataSs+0x12d9)[0x815b14d]
../src/afkmud(_Z12char_to_gameP9char_data+0x321)[0x817d013]
../src/afkmud(_ZN15descriptor_data5nannyERSs+0x446b)[0x8184007]
../src/afkmud(_Z13process_inputv+0x29e)[0x81563d4]
../src/afkmud(_Z9game_loopv+0x6f)[0x8156af0]
../src/afkmud(main+0x208)[0x81576fc]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x540b56]
../src/afkmud[0x807cb41]
======= Memory map: ========
00110000-00112000 r-xp 00000000 08:01 1958 /lib/tls/i686/cmov/libdl-2.10.1.so
00112000-00113000 r--p 00001000 08:01 1958 /lib/tls/i686/cmov/libdl-2.10.1.so
00113000-00114000 rw-p 00002000 08:01 1958 /lib/tls/i686/cmov/libdl-2.10.1.so
00114000-00138000 r-xp 00000000 08:01 1960 /lib/tls/i686/cmov/libm-2.10.1.so
00138000-00139000 r--p 00023000 08:01 1960 /lib/tls/i686/cmov/libm-2.10.1.so
00139000-0013a000 rw-p 00024000 08:01 1960 /lib/tls/i686/cmov/libm-2.10.1.so
0013a000-00264000 r-xp 00000000 08:01 8534 /usr/lib/libX11.so.6.2.0
00264000-00265000 ---p 0012a000 08:01 8534 /usr/lib/libX11.so.6.2.0
00265000-00266000 r--p 0012a000 08:01 8534 /usr/lib/libX11.so.6.2.0
00266000-00268000 rw-p 0012b000 08:01 8534 /usr/lib/libX11.so.6.2.0
00268000-00269000 rw-p 00000000 00:00 0
0026c000-00287000 r-xp 00000000 08:01 158 /lib/ld-2.10.1.so
00287000-00288000 r--p 0001a000 08:01 158 /lib/ld-2.10.1.so
00288000-00289000 rw-p 0001b000 08:01 158 /lib/ld-2.10.1.so
00289000-00303000 r-xp 00000000 08:01 8846 /usr/lib/libfreetype.so.6.3.20
00303000-00307000 r--p 00079000 08:01 8846 /usr/lib/libfreetype.so.6.3.20
00307000-00308000 rw-p 0007d000 08:01 8846 /usr/lib/libfreetype.so.6.3.20
00308000-0030a000 r-xp 00000000 08:01 8540 /usr/lib/libXau.so.6.0.0
0030a000-0030b000 r--p 00001000 08:01 8540 /usr/lib/libXau.so.6.0.0
0030b000-0030c000 rw-p 00002000 08:01 8540 /usr/lib/libXau.so.6.0.0
0030c000-00310000 r-xp 00000000 08:01 8551 /usr/lib/libXdmcp.so.6.0.0
00310000-00311000 rw-p 00003000 08:01 8551 /usr/lib/libXdmcp.so.6.0.0
00311000-00326000 r-xp 00000000 08:01 2024 /lib/tls/i686/cmov/libpthread-2.10.1.so
00326000-00327000 r--p 00014000 08:01 2024 /lib/tls/i686/cmov/libpthread-2.10.1.so
00327000-00328000 rw-p 00015000 08:01 2024 /lib/tls/i686/cmov/libpthread-2.10.1.so
00328000-0032a000 rw-p 00000000 00:00 0
003b5000-003c5000 r-xp 00000000 08:01 8571 /usr/lib/libXpm.so.4.11.0
003c5000-003c6000 r--p 0000f000 08:01 8571 /usr/lib/libXpm.so.4.11.0
003c6000-003c7000 rw-p 00010000 08:01 8571 /usr/lib/libXpm.so.4.11.0
003cb000-003e9000 r-xp 00000000 08:01 67185 /usr/lib/libgd.so.2.0.0
003e9000-003ea000 r--p 0001d000 08:01 67185 /usr/lib/libgd.so.2.0.0
003ea000-00409000 rw-p 0001e000 08:01 67185 /usr/lib/libgd.so.2.0.0
00409000-0040d000 rw-p 00000000 00:00 0
0047d000-0047e000 r-xp 00000000 00:00 0 [vdso]
00514000-00528000 r-xp 00000000 08:01 2033 /lib/libz.so.1.2.3.3
00528000-00529000 r--p 00013000 08:01 2033 /lib/libz.so.1.2.3.3
00529000-0052a000 rw-p 00014000 08:01 2033 /lib/libz.so.1.2.3.3
0052a000-00668000 r-xp 00000000 08:01 1949 /lib/tls/i686/cmov/libc-2.10.1.so
00668000-00669000 ---p 0013e000 08:01 1949 /lib/tls/i686/cmov/libc-2.10.1.so
00669000-0066b000 r--p 0013e000 08:01 1949 /lib/tls/i686/cmov/libc-2.10.1.so
0066b000-0066c000 rw-p 00140000 08:01 1949 /lib/tls/i686/cmov/libc-2.10.1.so
0066c000-0066f000 rw-p 00000000 00:00 0
006bd000-006d9000 r-xp 00000000 08:01 9572 /usr/lib/libxcb.so.1.1.0
006d9000-006da000 r--p 0001c000 08:01 9572 /usr/lib/libxcb.so.1.1.0
006da000-006db000 rw-p 0001d000 08:01 9572 /usr/lib/libxcb.so.1.1.0
00837000-0085d000 r-xp 00000000 08:01 9343 /usr/lib/libpng12.so.0.37.0
0085d000-0085e000 r--p 00025000 08:01 9343 /usr/lib/libpng12.so.0.37.0
0085e000-0085f000 rw-p 00026000 08:01 9343 /usr/lib/libpng12.so.0.37.0
009ca000-009ec000 r-xp 00000000 08:01 9171 /usr/lib/libjpeg.so.62.0.0
009ec000-009ed000 r--p 00021000 08:01 9171 /usr/lib/libjpeg.so.62.0.0
009ed000-009ee000 rw-p 00022000 08:01 9171 /usr/lib/libjpeg.so.62.0.0
00c93000-00cb7000 r-xp 00000000 08:01 37801 /lib/libexpat.so.1.5.2
00cb7000-00cb9000 r--p 00024000 08:01 37801 /lib/libexpat.so.1.5.2
00cb9000-00cba000 rw-p 00026000 08:01 37801 /lib/libexpat.so.1.5.2
00ce5000-00dcb000 r-xp 00000000 08:01 7392 /usr/lib/libstdc++.so.6.0.13
00dcb000-00dcf000 r--p 000e6000 08:01 7392 /usr/lib/libstdc++.so.6.0.13
00dcf000-00dd0000 rw-p 000ea000 08:01 7392 /usr/lib/libstdc++.so.6.0.13
00dd0000-00dd7000 rw-p 00000000 00:00 0
00e47000-00e63000 r-xp 00000000 08:01 5296 /lib/libgcc_s.so.1
00e63000-00e64000 r--p 0001b000 08:01 5296 /lib/libgcc_s.so.1
00e64000-00e65000 rw-p 0001c000 08:01 5296 /lib/libgcc_s.so.1
00f26000-00f51000 r-xp 00000000 08:01 8838 /usr/lib/libfontconfig.so.1.3.0
00f51000-00f52000 r--p 0002a000 08:01 8838 /usr/lib/libfontconfig.so.1.3.0
00f52000-00f53000 rw-p 0002b000 08:01 8838 /usr/lib/libfontconfig.so.1.3.0
08048000-08348000 r-xp 00000000 08:01 332484 /home/vmplanet/Downloads/afkmud/src/afkmud
08348000-08349000 r--p 002ff000 08:01 332484 /home/vmplanet/Downloads/afkmud/src/afkmud
08349000-0834d000 rw-p 00300000 08:01 332484 /home/vmplanet/Downloads/afkmud/src/afkmud
0834d000-084a4000 rw-p 00000000 00:00 0
08d31000-08f93000 rw-p 00000000 00:00 0 [heap]
b778c000-b7791000 rw-p 00000000 00:00 0
b77a8000-b77aa000 rw-p 00000000 00:00 0
bf99e000-bf9b3000 rw-p 00000000 00:00 0 [stack]
Aborted


Its fairly ugly to look at and I'm completely new to linux. I've done all my compiling through cygwin on Windows for years, so I'm not sure what I'm looking at here. I had assumed it had to do with the NMOTD, but after looking in the help files it looks perfectly normal to me. The crash occurs every time I get to that step in creation though. Also, when I use Admin, I can't get hlist to work. So maybe there is an error involving my helpfiles? Any helps or pointers for a new linux user would be greatly appreciated. If you need to see my compile errors I can post those as well.
       
Post is unread #2 Mar 6, 2010, 5:55 am
Go to the top of the page
Go to the bottom of the page

tphegley
Magician
GroupMembers
Posts176
JoinedMay 21, 2006

show_map_char_data looks like the spot that's messed up. If you aren't getting a clean compile then you should probably start there. You'll definitely need to post your compiling errors if you need more help.
       
Post is unread #3 Mar 6, 2010, 9:53 am
Go to the top of the page
Go to the bottom of the page

Riffic
Fledgling
GroupMembers
Posts13
JoinedApr 15, 2009

Alright, I'll just post the parts that are compiling wrong. I've fixed everything I could figure out on my own. After the show_map_char_data was pointed out I can see it on the crash log. Its a bit different then reading cygwin but I'm sure I'll get it in time. Here are my compiling errors:

Compiling o/boards.o....
boards.cpp: In function ‘void load_boards()’:
boards.cpp:1132: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

Compiling o/comm.o....
comm.cpp: In function ‘void directory_check()’:
comm.cpp:234: warning: ignoring return value of ‘int chdir(const char*)’, declared with attribute warn_unused_result
comm.cpp:241: warning: ignoring return value of ‘int chdir(const char*)’, declared with attribute warn_unused_result
comm.cpp:242: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

Compiling o/hotboot.o....
hotboot.cpp: In function ‘void hotboot_recover()’:
hotboot.cpp:792: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result

Compiling o/object.o....
new_auth.cpp: In function ‘void name_generator(std::string&;)’:
new_auth.cpp:76: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp:80: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp:86: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp:93: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp:100: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp: In function ‘void pick_name(std::string&, const char*)’:
new_auth.cpp:146: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp:150: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
new_auth.cpp:155: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result

Compiling o/pfiles.o....
pfiles.cpp: In function ‘void do_pfiles(char_data*, std::string)’:
pfiles.cpp:854: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result
pfiles.cpp: In function ‘void check_pfiles(time_t)’:
pfiles.cpp:907: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result


I noticed in the object.o and the pfiles.o that there are references to characters so maybe that's the reason things are happening when I create a new character, but I'm not seeing anything wrong in the code itself.
       
Post is unread #4 Mar 6, 2010, 4:13 pm
Go to the top of the page
Go to the bottom of the page

tphegley
Magician
GroupMembers
Posts176
JoinedMay 21, 2006

Try to run your game in gdb and that will tell you the exact spot that's causing the crash.

In the area file, type in:

gdb ../src/afk

Type run after this.

Check this site for additional details. www.mushclient.com/gdb
       
Post is unread #5 Mar 6, 2010, 5:02 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

_Z8show_mapP9char_dataPc+0x301 is the c++ mangled function name for the show_map() function. That's found in mapper.cpp and is used to generate the local map display in each room. There's more than one call to strcpy in there so without having a live backtrace from gdb to look at, it would be hard to say which one caused it. That code was written by Zarius and I never really had a firm grasp on how it worked because it uses C strings. So any insight into the problem would be helpful.

The warnings you're getting are no doubt because of more strictness from the GCC guys. What version of the compiler are you running?
       
Post is unread #6 Mar 6, 2010, 5:18 pm
Go to the top of the page
Go to the bottom of the page

Riffic
Fledgling
GroupMembers
Posts13
JoinedApr 15, 2009

Well, the first thing I did was to read through the site you linked to figure out gdb. Then I went into my make file and commented out the OPT-FLAG. I switched the debug flag from g2 to g3 and did a make clean. It compiled perfectly. Yay! So thanks a ton for pointing that out. I started to boot afkmud up through gdb the way you said and now it won't go past loading helps. I thought it might be gdb so tried booting it up like normal through the terminal and it still won't go past the helps. Just sits there hung up. I've tried going through the src folder and running it ./afkmud 4000& to try to get it up and still the same hangup.
       
Post is unread #7 Mar 6, 2010, 5:24 pm
Go to the top of the page
Go to the bottom of the page

Riffic
Fledgling
GroupMembers
Posts13
JoinedApr 15, 2009

I'm using version 4.4.1 of the compiler and I think I've installed most of the packages yesterday just to get it to compile the mud and run kmuddy to log in.
       
Post is unread #8 Mar 6, 2010, 11:00 pm
Go to the top of the page
Go to the bottom of the page

Riffic
Fledgling
GroupMembers
Posts13
JoinedApr 15, 2009

Alright, I decided to try a complete clean install of afkmud on my Ubuntu. I had unpacked it and compiled it via gnome before I had installed any libs and devs. I've also installed kds and am running everything through that now too since I heard it was easier for windows people. Anyway, I unpacked afkmud with gzip this time instead of my compiler and edited the makefile to comment out OPT-FLAG again and left the debug flag at the g2 just to see. I make clean and it compiled perfectly again. This time I went to boot it up and it booted perfectly! So I'm happy. I logged in to create a new character and didn't get my buffer overflow this time. The only two things that I've found annoying is I couldn't access the helpfiles before, and now I can, but only if I type them in complete capital letters. Help START works where help start doesn't. Also, when you enter a new name it doesn't do anything at all until you hit enter a second time, then it proceeds to ask for a confirmation. Other then that, I think I'm finally good to go! Thanks for the help, I appreciate it tons as always. :)
       
Post is unread #9 Mar 7, 2010, 10:24 am
Go to the top of the page
Go to the bottom of the page

tphegley
Magician
GroupMembers
Posts176
JoinedMay 21, 2006

I have noticed the capitalization thing as well. I have notified Samson of it in the past so it will probably be taken care of and the case fix will be applied.
       
Post is unread #10 Mar 7, 2010, 1:33 pm
Go to the top of the page
Go to the bottom of the page

Quixadhal
Conjurer
GroupMembers
Posts398
JoinedMar 8, 2005

Yep, same here... not sure if I sent him a patch or not (I was also poking at the SQL code that's fallen out of date)... too many pans in the fire of late. :)

It's fallout from the std::string conversion, because the C++ standards commies stupidly didn't provide a case insensitive option for checking equality between std::string objects, so you have to do it yourself by mucking about with std::transform or something similar.
       
Post is unread #11 Mar 8, 2010, 1:36 am
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

It's easier to just expand on std::string. and provide case insensitive variants inside your own namespace, kinda like I did with Elysium.

//std::string case insensitive modifications
namespace Elysium
{
   int ElysiumCharTraits::compare( const char *s1, const char *s2, size_t n ) 
   { 
      return strncmp( s1, s2, n ); 
   }

   const char *ElysiumCharTraits::find( const char *s, size_t n, const char &a ) 
   { 
      char b = tolower( a ); 
      while( n-- ) 
      { 
         if ( tolower( *s ) == b ) 
            return s; 
         ++s; 
      } 
      return NULL; 
   }
}
 //End case insensitivity modifications


#include <string>

//std::string case insensitive modifications
namespace Elysium
{
   struct ElysiumCharTraits : public std::char_traits<char>
   {
      static bool eq( const char &c1, const char &c2 ) { return static_cast< bool > ( tolower( c1 ) == tolower( c2 ) ); }
      static bool lt( const char &c1, const char &c2 ) { return static_cast< bool > ( tolower( c1 ) == tolower( c2 ) ); }
 
      static int compare( const char *s1, const char *s2, size_t n );
      static const char *find( const char *s, size_t n, const char &a );
   }; //ElysiumCharTraits

   typedef std::basic_string< char, ElysiumCharTraits, std::allocator< char > > String;
} //namespace
//End case insensitivity modifications.


Something like that.
       
Post is unread #12 Mar 8, 2010, 2:35 am
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

It would probably take a huge effort to get AFKMud into any kid of shape to use class extensions that way. Much as I'd like to. So I settled for what Quixadhal provided which solves the problem with the help files.
       
Post is unread #13 Mar 8, 2010, 2:12 pm   Last edited Mar 8, 2010, 2:15 pm by Kayle
Go to the top of the page
Go to the bottom of the page

Kayle
Off the Edge of the Map
GroupAdministrators
Posts1,195
JoinedMar 21, 2006

Er... No? Well, unless you did something silly like put 'using namespace std;' in a Header file somewhere. Which would causes isses. But really. It's easy. You take the code I posted, put it in a .h and .cpp file, and change it from Elysium to AFKMud. So like...

stringmods.h
#ifndef __STRING_MODS_H__
#define __STRING_MODS_H__

#include <string>

//std::string case insensitive modifications
namespace AFKMud
{
   struct AFKMudCharTraits : public std::char_traits<char>
   {
      static bool eq( const char &c1, const char &c2 ) { return static_cast< bool > ( tolower( c1 ) == tolower( c2 ) ); }
      static bool lt( const char &c1, const char &c2 ) { return static_cast< bool > ( tolower( c1 ) == tolower( c2 ) ); }
 
      static int compare( const char *s1, const char *s2, size_t n );
      static const char *find( const char *s, size_t n, const char &a );
   }; //AFKMudCharTraits

   typedef std::basic_string< char, AFKMudCharTraits, std::allocator< char > > String;
} //namespace
//End case insensitivity modifications.

#endif


And stringmods.cpp
#include <string.h>
#include "stringmods.h"

//std::string case insensitive modifications
namespace AFKMud
{
   int AFKMudCharTraits::compare( const char *s1, const char *s2, size_t n ) 
   { 
      return strncmp( s1, s2, n ); 
   }

   const char *AFKMudCharTraits::find( const char *s, size_t n, const char &a ) 
   { 
      char b = tolower( a ); 
      while( n-- ) 
      { 
         if ( tolower( *s ) == b ) 
            return s; 
         ++s; 
      } 
      return NULL; 
   }
}
 //End case insensitivity modifications


And then replace all calls to std::string with AFKMud::String. Then Viola. Case Insensitive std::string implementation.

[Edit:] Oh, and you'd include "stringmods.h" everywhere instead of <string>
       
Post is unread #14 Mar 8, 2010, 3:36 pm
Go to the top of the page
Go to the bottom of the page

Quixadhal
Conjurer
GroupMembers
Posts398
JoinedMar 8, 2005

The problem is, you don't ALWAYS want strings to be case insensitive, and C++ won't let you define arbitrary operators.... "==" has to always point at one function for a given pair of types to compare. It can't know that in the command parser, I want to compare case-insensitive, but in some other data-manipulation function, I need to to be exact equality.

Now, if you were to define a new string type (call it std::istring), then you could do things like:
string s;
istring t;

s = "Hello"
t = "hello"

if( s == "Hello" ) // true
if( s == "hello" ) // false
if( t == "Hello" ) // true
if( (istring)s == (istring)"hello";) // true
if( s == t ) // false
if( (istring)s == t ) // true

       
Post is unread #15 Mar 8, 2010, 3:48 pm   Last edited Mar 8, 2010, 4:14 pm by Samson
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Kayle said:

Er... No? Well, unless you did something silly like put 'using namespace std;' in a Header file somewhere.


You mean like how I did in mud.h? :P

Hindsight is a wonderful thing, because I look at that now and go "WTF was I thinking". It seemed like a good idea at the time though. Just like how I didn't make much use of "this->whatever" instead of just "whatever" when inside class functions. I'm sure it's pretty confusing to other people since I look at some of that now and get confused myself.
       
Post is unread #16 Mar 9, 2010, 8:14 am   Last edited Mar 9, 2010, 8:16 am by David Haley
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

Viola

Stop playing fiddles and say "voila" instead. :evil:

if( (istring)s == (istring)"hello";) // true

istring(s) and istring("hello";) are more conventional notations, but yes.

Just like how I didn't make much use of "this->whatever" instead of just "whatever" when inside class functions. I'm sure it's pretty confusing to other people since I look at some of that now and get confused myself.

Well, I don't like to use 'this' everywhere myself since I find it overly verbose. I use a naming convention that makes it clear which variables are member variables, e.g., this->foobar_ instead of this->foobar, which means that I just write foobar_ and it's obvious that it's a member variable. Some people prefer things like m_foobar.
       
Post is unread #17 Mar 9, 2010, 12:14 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

David Haley said:

Some people prefer things like m_foobar.


I'm understanding why that is now too, but I'd never do that because it grates on me for some reason. I'd much rather just use this->foobar.
       
Post is unread #18 Mar 9, 2010, 1:28 pm
Go to the top of the page
Go to the bottom of the page

David Haley
Sorcerer
GroupMembers
Posts903
JoinedJan 29, 2007

I'm not big a fan of the m_foobar notation either. When I can't use naming conventions, I usually drop the this-> part unless it helps for clarity. E.g.,

void Class::foo(int i) {
  this->i = i;
}


rather than:

void Class::foo(int ival) {
  i = ival;
}


which I find harder to read.

Then again, after writing so much Python code these days, I'm very used to seeing self.membervar, so I wouldn't mind the 'this' as much, probably.
       
Post is unread #19 Apr 9, 2010, 5:53 am
Go to the top of the page
Go to the bottom of the page

Hygelac
Fledgling
GroupMembers
Posts3
JoinedMay 29, 2009

This code is not going to work b/c strncmp() does case sensitive compares.
Kayle said:


//std::string case insensitive modifications
namespace Elysium
{
   int ElysiumCharTraits::compare( const char *s1, const char *s2, size_t n ) 
   { 
      return strncmp( s1, s2, n ); 
   }



You can use the nonstandard functions strnicmp() or memicmp(). Or you can roll your own
int ElysiumCharTraits::compare( const char *s1, const char *s2, size_t n )
{
   while( n-- )
   {
      char r = tolower( *s1 ) - tolower( *s2 );
      if( r ) return r;
      ++s1; ++s2;
   }
   return 0;
}


However you still have the following problems:
Elysium::String es( "hello";);
std:string s("hello";);

// obviously case sensitive comparisons can be done via
assert( strcmp( es.c_str(), "HeLlO" ) != 0 );
assert( strcmp( es.c_str(), "hello" ) == 0 );


// will work
cout << s;
s += s;
es += es;

// not going to work
cout << es; 
s += es;
es += s;


So you don't really have string that behaves like std::string unless you write several wrapper functions to do: <<, +=, >>, etc. It's a lot easier to just write the function stringicmp( string const &a, string const &b ); and use that.


- Hygelac
----
The Legends of Xenobia at telnet://xenobia.mudmagic.com:1972
       
Pages:<< prev 1 next >>