TinTin++ Mud Client The TinTin++ message board

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
TinTin++ Mud Client

[Bug:] Map resize not initialising roomlist when extended...

 
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
Slysven



Joined: 10 Apr 2011
Posts: 375
Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

PostPosted: Thu Jan 03, 2013 4:07 pm    Post subject: [Bug:] Map resize not initialising roomlist when extended... Reply with quote

I was hacking around to see if I could "import" chunks from data extracted from someone else's MS Jet Database based ZMud file into my own map file and I spotted this issue with the map_resize() function regarding the use of realloc:

In malloc(3) man page was wrote:
DESCRIPTION

... realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. ...


This patch (this is against the 2.00.9 release) should fix it:

Code:
--- mapper.c   2013-01-01 20:51:45.000000000 +0000
+++ mapper.c.new   2013-01-03 20:44:35.000000000 +0000
@@ -1450,9 +1450,15 @@
       }
    }
 
-   ses->map->size = size;
+   ses->map->room_list = (struct room_data **) realloc(ses->map->room_list, size * sizeof(struct room_data *));
+
+   if (size > ses->map->size)
+   {
+      for (vnum = ses->map->size + 1; vnum <= size ; vnum++)
+         ses->map->room_list[vnum] = NULL;      
+   }
 
-   ses->map->room_list = (struct room_data **) realloc(ses->map->room_list, ses->map->size * sizeof(struct room_data *));
+   ses->map->size = size;
 
    show_message(ses, -1, "#MAP RESIZE: MAP RESIZED TO %d ROOMS.", size);
 }
I guess this might explain the odd crash I've had in the past when I have enlarged my maps...? Smile
Back to top
View user's profile Send private message
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3844

PostPosted: Thu Jan 03, 2013 7:32 pm    Post subject: Reply with quote

Nice catch.

The for loop is incorrect however, and should be:

Code:

for (vnum = ses->map->size ; vnum < size ; vnum++)


Not sure why this didn't result in a crash on your end.
Back to top
View user's profile Send private message Send e-mail
Slysven



Joined: 10 Apr 2011
Posts: 375
Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

PostPosted: Thu Jan 03, 2013 11:35 pm    Post subject: Reply with quote

Isn't room vnum = 0 the error case now (was -1). I'd previously noted that size_t - the "right" type for array indexing is UNSIGNED - though not currently used much (at all?) in the source code. Thus for n rooms the "used" room list index can range from 1 to n not 0 to n-1 and the first pointer (room_list[0]) in the array is unused - for simplicity? That being said, I guess I've just been lucky so far and the memory alignment of that returned by the realloc() call has avoided a buffer overflow violation on using an n'th pointer. Perhaps the "size" in the realloc call should be (size+1)... Confused Mind you, could there be places where what ses->map->room_list[0] points to is considered when perhaps it should not {closer examination indicates that this will be a NULL pointer as it IS initialised by calloc() in create_map() and thus gets ignored or otherwise treated as appropriate in code that uses the room_list array}.
Back to top
View user's profile Send private message
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3844

PostPosted: Fri Jan 04, 2013 7:35 am    Post subject: Reply with quote

room_list[0] was always reserved as an error case. The function using -1 slipped in out of habit.

It's custom to create an array using 'size' and have it's range be between 0 and size-1, so I'm not going to mess with that, for reasons of my own sanity alone.
Back to top
View user's profile Send private message Send e-mail
Slysven



Joined: 10 Apr 2011
Posts: 375
Location: As "Jomin al'Bara" in WoTMUD or Wiltshire, UK

PostPosted: Fri Jan 04, 2013 1:22 pm    Post subject: Reply with quote

I hear you Bro! Laughing

Though someone, sometime, somewhere is gonna ask "I asked for and created a 100 room map, why have I only got 99?" Smirk
Back to top
View user's profile Send private message
Scandum
Site Admin


Joined: 03 Dec 2004
Posts: 3844

PostPosted: Fri Jan 04, 2013 10:29 pm    Post subject: Reply with quote

I'll have to tell them one room is reserved as my private abode. Totally Sweet
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    The TinTin++ message board Forum Index -> Bug Reports All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads Get TinTin++ Mud Client at SourceForge.net. Fast, secure and Free Open Source software downloads
TinTin++ Homepage

Powered by phpBB © 2001, 2002 phpBB Group