check_social question

Posted by Toy on Fri 18 Jun 2004 09:31 PM — 5 posts, 18,501 views.

#0
bool check_social( CHAR_DATA *ch, char *command, char *argument )
{
    	char arg[MAX_INPUT_LENGTH];
    	char buf[MAX_INPUT_LENGTH];
    	CHAR_DATA *victim; *** THIS LINE IS THE ERROR ***
    	SOCIALTYPE *social;
	CHAR_DATA *remfirst, *remlast, *remtemp;      /* for ignore cmnd */

    	if ( (social=find_social(command)) == NULL )
		return FALSE;

    	if ( !IS_NPC(ch) && xIS_SET(ch->act, PLR_NO_EMOTE) )
    	{
		send_to_char( "You are anti-social!\n\r", ch );
		return TRUE;
    	}
   
    	switch ( ch->position )
    	{
    		case POS_DEAD:
			send_to_char( "Lie still; you are DEAD.\n\r", ch );
			return TRUE;

    		case POS_INCAP:
    		case POS_MORTAL:
			send_to_char( "You are hurt far too bad for that.\n\r", ch );
			return TRUE;

    		case POS_STUNNED:
			send_to_char( "You are too stunned to do that.\n\r", ch );
			return TRUE;

    		case POS_SLEEPING:
/* I just know this is the path to a 12" 'if' statement.  :( But two players asked for it already! -- Furey */
			if ( !str_cmp( social->name, "snore" ) )
	    			break;
	
			send_to_char( "In your dreams, or what?\n\r", ch );
			return TRUE;
    	}

    	remfirst = NULL;
    	remlast = NULL;
    	remtemp = NULL;
    
    	one_argument( argument, arg );

    	if ( arg[0] == '\0' )
    	{
      		if( !social->others_no_arg || !social->char_no_arg)
      		{
        		send_to_char("Incomplete social, try again soon.\n\r", ch);
        		sprintf(buf, "Social %s", social->name);
        		bug(buf);
        		return TRUE;
      		}
	
		act( AT_SOCIAL, social->others_no_arg, ch, NULL, victim, TO_ROOM    );
		act( AT_SOCIAL, social->char_no_arg,   ch, NULL, victim, TO_CHAR    );
    	}
    	else if ( ( victim = get_char_world( ch, arg ) ) == NULL )
    	{
    		for(victim = remfirst; victim; victim = victim->next_in_room)
    		{
    			if(nifty_is_name(victim->name,arg) || nifty_is_name_prefix(arg,victim->name))
    			{
    				set_char_color(AT_IGNORE, ch);
    				ch_printf(ch,"%s is ignoring you.\n\r", victim->name);
    				break;
    			}
    		}
    	}
    	else if((victim = get_char_world(ch, arg)) != NULL && !IS_NPC(victim) 
       	&& (victim->in_room->vnum != ch->in_room->vnum))
    	{
      		if( !social->vict_found || !social->char_found)
      		{
        		send_to_char("Incomplete social, try again soon.\n\r", ch);
        		sprintf(buf, "Social %s", social->name);
        		bug(buf);
        		return TRUE;
      		}
      
      		strcpy( buf, "From afar...");
      		strcat( buf, social->vict_found );
      		act( AT_SOCIAL, buf, ch, NULL, victim, TO_VICT );
      		strcpy( buf, "From afar...");
      		strcat( buf, social->char_found);
      		act( AT_SOCIAL, buf, ch, NULL, victim, TO_CHAR );
    	}
    	else if(victim != NULL && !IS_NPC(victim) && victim != ch)
    	{
      		if( !social->vict_found || !social->char_found || !social->others_found)
      		{
        		send_to_char("Incomplete social, try again soon.\n\r", ch);
        		sprintf(buf, "Social %s", social->name);
        		bug(buf);
        		return TRUE;
      		}
      
      		act( AT_SOCIAL, social->vict_found,    ch, NULL, victim, TO_VICT    );
      		act( AT_SOCIAL, social->char_found,    ch, NULL, victim, TO_CHAR    );
      		act( AT_SOCIAL, social->others_found,  ch, NULL, victim, TO_NOTVICT );
    	}
Amended on Fri 18 Jun 2004 09:33 PM by Toy
#1
pt 2:
    	else if ( victim == ch )
    	{
      		if( !social->char_auto || !social->others_auto)
      		{
        		send_to_char("Incomplete social, try again soon.\n\r", ch);
        		sprintf(buf, "Social %s", social->name);
        		bug(buf);
        		return TRUE;
      		}
	
		act( AT_SOCIAL, social->others_auto,   ch, NULL, victim, TO_ROOM    );
		act( AT_SOCIAL, social->char_auto,     ch, NULL, victim, TO_CHAR    );
    	}
    	else
    	{
      		if( !social->char_found || !social->others_found || !social->vict_found)
      		{
        		send_to_char("Incomplete social, try again soon.\n\r", ch);
        		sprintf(buf, "Social %s", social->name);
        		bug(buf);
        		return TRUE;
      		}
	
		act( AT_SOCIAL, social->others_found,  ch, NULL, victim, TO_NOTVICT );
		act( AT_SOCIAL, social->char_found,    ch, NULL, victim, TO_CHAR    );
		act( AT_SOCIAL, social->vict_found,    ch, NULL, victim, TO_VICT    );

		if ( !IS_NPC(ch) && IS_NPC(victim)
		&&   !IS_AFFECTED(victim, AFF_CHARM)
		&&   IS_AWAKE(victim) 
		&&   !HAS_PROG(victim->pIndexData, ACT_PROG) )
		{
	    
	    		switch ( number_bits( 4 ) )
	    		{
	    
	    			case 0:
					if (IS_EVIL(ch) && !is_safe(victim, ch, TRUE)) 
/* was IS_EVIL(ch) ||.... didn't make sense to me - FB */
		  				multi_hit( victim, ch, TYPE_UNDEFINED );
					else
					if ( IS_NEUTRAL(ch) )
					{
		    				act( AT_ACTION, "$n slaps $N.",  victim, NULL, ch, TO_NOTVICT );
		    				act( AT_ACTION, "You slap $N.",  victim, NULL, ch, TO_CHAR    );
		    				act( AT_ACTION, "$n slaps you.", victim, NULL, ch, TO_VICT    );
					}
					else
					{
		    				act( AT_ACTION, "$n acts like $N doesn't even exist.",  victim, NULL, ch, TO_NOTVICT );
		    				act( AT_ACTION, "You just ignore $N.",  victim, NULL, ch, TO_CHAR    );
		    				act( AT_ACTION, "$n appears to be ignoring you.", victim, NULL, ch, TO_VICT    );
					}
					break;

	    				case 1: case 2: case 3: case 4:
	    				case 5: case 6: case 7: case 8:
						act( AT_SOCIAL, social->others_found, victim, NULL, ch, TO_NOTVICT );
						act( AT_SOCIAL, social->char_found, victim, NULL, ch, TO_CHAR    );
						act( AT_SOCIAL, social->vict_found, victim, NULL, ch, TO_VICT    );
						break;

	    				case 9: case 10: case 11: case 12:
						act( AT_ACTION, "$n slaps $N.",  victim, NULL, ch, TO_NOTVICT );
						act( AT_ACTION, "You slap $N.",  victim, NULL, ch, TO_CHAR    );
						act( AT_ACTION, "$n slaps you.", victim, NULL, ch, TO_VICT    );
						break;
	    		}
		}
    	}
    
/* Replace the chars in the ignoring list to the room 
   note that the ordering of the players in the room might change */
    	for(victim = remfirst; victim; victim = remtemp)
    	{
    		remtemp = victim->next_in_room;
    		char_to_room(victim, ch->in_room);
    	}

    	return TRUE;
}

I'm getting this error:
interp.c: In function `check_social':
interp.c:814: warning: `victim' might be used uninitialized in this function

I've marked the line in error at the very top of the first post.

-Toy
Australia Forum Administrator #2
It's a warning, not an error. Note the word "warning". There is a distinction. This means it compiled but may not work as you expect.

The problem is that further down you do this:


act( AT_SOCIAL, social->others_no_arg, ch, NULL, victim, TO_ROOM );


However you never set "victim" to be anything, thus it will be uninitialised data, and the server will probably crash. It is warning you of this.
USA #3
I've had it before as well. To get rid of it I merely did this:

CHAR_DATA *victim = NULL;

This can be very fatal if you are not sure what you are doing, so make sure you thrown in your if checks and what not to make sure that if its NULL then it does something like returning out of the function to prevent a crash.
USA #4
The other advantage to initializing it to NULL is that it's easier to track down the problem if it does crash - instead of having what could have been a useful character (but you don't know because it has apparently random information that may have been corrupted) you *know* that it was never set to something useful.