Thread: Client switching stack / invalid write/read size valgrind

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    5

    Client switching stack / invalid write/read size valgrind

    Here is the code:
    Code:
    void loadMap(char *map)
    {
      FILE *fp;
      char pfile[MAX_BUFFER];
      char *word;
      char *MAP;
      bool done = FALSE, found;
      TURF_DATA *turf;
    
      snprintf(pfile, MAX_BUFFER, "../area/%s", map);
      fp = fopen(pfile, "r");
    
      if (!fp) {
        bug("Failed to open map.");
        return;
      }
    
      AREA_DATA *newArea = createArea();
      MAP_DATA *newMap = createMap();
    
      word = fread_word(fp);
      while (!done) {
        found = FALSE;
        switch (word[0]) {
        case 'N':
          SREAD("Name", newArea->name);
          break;
        case 'G':
          IREAD("Gravity", newArea->gravity);
          break;
        case 'W':
          IREAD("Wrap", newArea->wrap);
          IREAD("Width", newMap->width);
          break;
        case 'L':
          IREAD("Locked", newArea->locked);
          break;
        case 'H':
          IREAD("Height", newMap->height);
          break;
        case 'M':
          IREAD("MapNumber", newMap->z);
          MREAD("Map", MAP);
          break;
        case 'E':
          if (!strcasecmp(word, "EOF") && !found) {
            done = TRUE;
            break;
          }
          break;
        }
    
        if (!done)
          word = fread_word(fp);
      }
    
      fclose(fp);
    
      newMap->area = newArea;
      int ROOMS = totalRooms(newMap);
      int x = 1, y = 1;
    
      listPut(map_list, newMap);
    
      int numbers[ROOMS];
      int k = 0;
      char *n = strtok(MAP, " ");
      do {
        int convertedNum = atoi(n);
        numbers[k++] = convertedNum;
      } while ((n = strtok(NULL, " ")));
    
      for (int i = 0; i != ROOMS; i++) {
        //bug("map:%s (%i)",newMap->area->name, numbers);
        turf = turfType(numbers, newMap);
        turf->x = x;
        turf->y = y;
        turf->z = newMap->z;
        setUniqueRoomID(turf, newMap);
    
        if (x++ == newMap->width) {
          x = 1;
          y++;
        }
      }
    
      if (newMap->z > HIGHEST_MAP_NUMBER) {
        HIGHEST_MAP_NUMBER = newMap->z;
      }
    }

    And here is the error thats being output in valgrind

    Code:
    rm -f ./DBZFE
    gcc -o ./DBZFE socket.o io.o strings.o utils.o interpret.o help.o action_safe.o                                                                                                                                                              mccp.o save.o list.o stack.o random.o dmob.o map.o skill.o common_skills.o mapob                                                                                                                                                             j.o combat.o load.o mob.o ai.o protocol.o kiattack.o worldthreads.o item.o form.                                                                                                                                                             o saiyan_skills.o mysql_functions.o -lz -lpthread -lcrypt -lm -lcrypto -L/usr/li                                                                                                                                                             b64/mysql/ -lmysqlclient
    F[dbzfe@ip197 src]$ valgrind ./DBZFE
    ==15108== Memcheck, a memory error detector
    ==15108== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
    ==15108== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
    ==15108== Command: ./DBZFE
    ==15108==
    ==15108== Warning: client switching stacks?  SP change: 0x1ffeff4b20 --> 0x1ffec                                                                                                                                                             24210
    ==15108==          to suppress, use: --max-stackframe=4000016 or greater
    ==15108== Invalid write of size 8
    ==15108==    at 0x419BD5: loadMap (map.c:356)
    ==15108==    by 0x4197DC: loadMaps (map.c:282)
    ==15108==    by 0x402483: main (socket.c:90)
    ==15108==  Address 0x1ffec24208 is on thread 1's stack
    ==15108==  in frame #0, created by loadMap (map.c:293)
    ==15108==
    ==15108== Invalid read of size 8
    ==15108==    at 0x626CE35: strtok (in /usr/lib64/libc-2.17.so)
    ==15108==    by 0x419BD9: loadMap (map.c:356)
    ==15108==    by 0x4197DC: loadMaps (map.c:282)
    ==15108==    by 0x402483: main (socket.c:90)
    ==15108==  Address 0x1ffec24208 is on thread 1's stack
    ==15108==  in frame #0, created by strtok (???:)
    ==15108==
    ==15108== Invalid write of size 4
    ==15108==    at 0x419C08: loadMap (map.c:359)
    ==15108==    by 0x4197DC: loadMaps (map.c:282)
    ==15108==    by 0x402483: main (socket.c:90)
    ==15108==  Address 0x1ffec24210 is on thread 1's stack
    ==15108==  in frame #0, created by loadMap (map.c:293)
    ==15108==
    ==15108== Invalid read of size 4
    ==15108==    at 0x419C3E: loadMap (map.c:364)
    ==15108==    by 0x4197DC: loadMaps (map.c:282)
    ==15108==    by 0x402483: main (socket.c:90)
    ==15108==  Address 0x1ffec24210 is on thread 1's stack
    ==15108==  in frame #0, created by loadMap (map.c:293)
    ==15108==
    ==15108== Warning: client switching stacks?  SP change: 0x1ffec24210 --> 0x1fff000358
    ==15108==          to suppress, use: --max-stackframe=4047176 or greater
    Waht am i doing wrong?
    Last edited by Salem; 10-04-2019 at 11:33 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > ==15108== at 0x419BD5: loadMap (map.c:356)
    I suppose it might help to know which actual line of code this corresponds to your snippet.

    > int numbers[ROOMS];
    Is ROOMS some stupid large number like 1000000 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    5
    Quote Originally Posted by Salem View Post
    > ==15108== at 0x419BD5: loadMap (map.c:356)
    I suppose it might help to know which actual line of code this corresponds to your snippet.

    > int numbers[ROOMS];
    Is ROOMS some stupid large number like 1000000 ?
    basically

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So what's your question?

    I could tell you to not use such large arrays, and to allocate it dynamically.

    I could tell you to read the valgrind message, and use the --max-stackframe like it tells you to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Valgrind invalid Read/Write
    By SAP_7 in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2016, 11:52 PM
  2. Replies: 2
    Last Post: 07-29-2013, 05:07 PM
  3. Valgrind - Invalid read of size 1
    By Castelmagno in forum C Programming
    Replies: 7
    Last Post: 02-29-2012, 03:19 PM
  4. Valgrind Invalid Read/Write In C Program
    By Alex Richman in forum C Programming
    Replies: 2
    Last Post: 10-02-2011, 03:15 PM
  5. Invalid Read from Valgrind
    By jduro in forum C Programming
    Replies: 1
    Last Post: 10-05-2010, 11:28 AM

Tags for this Thread