🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #137

Re: I am stumped by this one!!!

#137
From: rajeev.dolas
Date: Mon, 27 Jul 1992 13:48
94 lines
3253 bytes
In article <13744@auspex-gw.auspex.com> guy@Auspex.COM (Guy Harris) writes:
>>>		cp_line = (char *)malloc(sizeof(strlen(pars_wbuf)));
>>
>> You're allocing the sizeof an int, which is likely four bytes.
>> You also forgot to alloc space for the '\0'. Try it again with:
>>
>>	cp_line = (char*)malloc(sizeof(strlen(pars_wbuf) + 1));
>
>No, try it again with:
>
>>	cp_line = (char*)malloc(strlen(pars_wbuf) + 1);
>
>because, as you noted, the "sizeof" doesn't belong there - it turns the
>argument into the size of the result of "strlen", which isn't the length
>of the string.
>
>(Also, try not leaving every single line of the original posting in your
>followup; it makes it rather hard to find the stuff that *doesn't* come
>from the original posting....)

	I realize the error on sizeof instead of strlen() for malloc. But
	I don't think that is the cause of SEGV error. If you look at my
	original posting, you will notice that I am able to copy selected
	characters to cp_line.

	In my original code I wasn't even using the cp_line. I was reading
	a char pointed to by *pbuf and was writing that to the file. I
	got the SEGV error on the putc()!!! Now I am using fputs() to
	write a string instead of a char and I get the same error on
	fputs(). Also fputs(tmp_line,stdout) works fine while 
	fputs(tmp_line,fp1) does not!!! That's what is driving me nuts.
	
	I am including the relevant portion of the code to clarify my
	point and not to drive the 2400 baud-ers nuts :-)

	/* str_comp is my function which returns -1 if match is not found */
	while((fgets(pars_wbuf, 80, fp2) != NULL) &&
		((parse_ret = str_comp(cmp, pars_wbuf)) != 0))
	{
		pars_wbuf[strlen(pars_wbuf)] = '\0';
		pbuf = pars_wbuf;
		cp_line = (char *)malloc(sizeof(strlen(pars_wbuf)));
		strcpy(cp_line, "\0");
		tmp_line = cp_line;

		if((comp = line_comp(pars_wbuf)) == 0)
			;
		else 
		{
			/* Traverse thru pbuf, skip "
			while(*pbuf != '\0')
			{
				if(*pbuf != 0x0D)
				{
					/* I was writing a char at a time to
					   the file before. This part works
					   on Amdahl but not on a sun.	*/

					/**
					ch = *pbuf;
					fprintf(stdout, "%c", ch); 
					if((he = putc(ch, fp1)) < 0)
					{
					     printf("pars_conf: Write error\n");
						return(-13);
					}
					**/

					/* Now I save it to a buffer */
					*cp_line++ = *pbuf;
				}
				*pbuf++;
			}
			/* If I do a fputs to stdout, it works fine */
			/**
			fputs(tmp_line, stdout);
			fflush(stdout);
			**/

			/* dbxtool shows the following error on this instr -
			 signal SEGV in malloc at 0xf773634
			 malloc+0x150:	st	%o0, [%15]
			 */
			fputs(tmp_line, fp1);
		}
		free(cp_line);
	}
	
}


Thanks again

Raj Dolas.
Technical Details
Message-ID: <1992Jul27.134826.5592@cbfsb.cb.att.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!walter!att!cbfsb!cbnewsf.cb.att.com!rajeev
References: <1992Jul24.204944.18516@cbfsb.cb.att.com> <Brx394.4qL@undergrad.math.waterloo.edu> <13744@auspex-gw.auspex.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!walter!att!cbfsb!cbnewsf.cb.att.com!rajeev
From: rajeev@cbnewsf.cb.att.com (rajeev.dolas)
Subject: Re: I am stumped by this one!!!
Message-ID: <1992Jul27.134826.5592@cbfsb.cb.att.com>
Sender: news@cbfsb.cb.att.com
Organization: AT&T
References: <1992Jul24.204944.18516@cbfsb.cb.att.com> <Brx394.4qL@undergrad.math.waterloo.edu> <13744@auspex-gw.auspex.com>
Distribution: usa
Date: Mon, 27 Jul 1992 13:48:26 GMT
Lines: 95