[comp.sys.ibm.pc] MSC 5.0 Intrinsic Bug

nortond@mosys.UUCP (Daniel A. Norton) (12/22/87)

Copyright (c) 1987, Daniel A. Norton.  All Rights Reserved.

You are hereby granted free and unlimited use of this article
with the exception that neither Microsoft Corporation nor any
of its employees or subsidiaries may copy this article beyond
that which is automatically provided by USENET and USENET
software.  Such copying includes, but is not restricted to
printing, archiving, xerography, and manual transcription.


PRODUCT:

	MSC 5.0 OPTIMIZING COMPILER


SYMPTOMS:

	Use of intrinsic function strlen() in large data model
	(/AH, /AL or /AC) results in data loss.  If the intrinsic
	function strlen() is called to return the length of a
	string represented by an auto-storage or static-storage
	pointer, and the result is returned into an external or
	public variable, incorrect code is generated, resulting
	in loss of data.

	The error does not appear in the small or medium model.
	The error does not appear if the variable holding the
	returned value is in static or auto storage.  The error
	does not appear if the string is a static array (as
	opposed to a static pointer).  The error does not appear
	if the string is an external or public array or is is
	accessed through an external or public pointer.


WORKAROUND:

	In source code which has the described characteristics,
	insert the line:

		#pragma function(strlen)

	This statement suppresses the intrinsic and forces the
	library function _strlen to be invoked instead.


REPRODUCING THE PROBLEM:

EXAMPLE SOURCE:
	
	#include <string.h>
	
	extern int extvar;
	
	void dummyproc(arg)
	char *arg;
	{
	extvar = strlen(arg);
	}


COMPILER INVOCATION:

	cl -c -AL -Oi -Fa bug2.c


ASSEMBLY OUTPUT:

	;	Static Name Aliases
	;
		TITLE   bug2.c
		NAME    bug2
	
		.8087
	BUG2_TEXT	SEGMENT  WORD PUBLIC 'CODE'
	BUG2_TEXT	ENDS
	_DATA	SEGMENT  WORD PUBLIC 'DATA'
	_DATA	ENDS
	CONST	SEGMENT  WORD PUBLIC 'CONST'
	CONST	ENDS
	_BSS	SEGMENT  WORD PUBLIC 'BSS'
	_BSS	ENDS
	DGROUP	GROUP	CONST, _BSS, _DATA
		ASSUME  CS: BUG2_TEXT, DS: DGROUP, SS: DGROUP
	EXTRN	__acrtused:ABS
	EXTRN	__chkstk:FAR
	COMM FAR	_extvar:	BYTE:	 2
	CONST      SEGMENT
	$T20001	DW SEG _extvar 
	CONST      ENDS
	BUG2_TEXT      SEGMENT
		ASSUME	CS: BUG2_TEXT
	; Line 6
		PUBLIC	_dummyproc
	_dummyproc	PROC FAR
		push	bp
		mov	bp,sp
		xor	ax,ax
		call	FAR PTR __chkstk
		push	di
	; Line 7
	;	arg = 6
	; Line 8
		mov	es,$T20001		; *** DISCARDED
		les	di,DWORD PTR [bp+6]	;arg
		mov	cx,-1
		xor	ax,ax
		repnz	scasb
		not	cx
		dec	cx
		mov	WORD PTR es:_extvar,cx	; *** INVALID
	; Line 9
		pop	di
		pop	bp
		ret	
	
	_dummyproc	ENDP
	BUG2_TEXT	ENDS
	END
-- 
Daniel A. Norton				nortond@mosys.UUCP
c/o Momentum Systems Corporation	     ...uunet!mosys!nortond
2 Keystone Avenue
Cherry Hill, NJ   08003 			609/424-0734

swh@hpsmtc1.HP.COM (Steve Harrold) (12/23/87)

Re: Copyright on bug report

A copyright???!!!
Is this a joke??  If so please explain what's so funny.

---------------------
Steve Harrold			...hplabs!hpsmtc1!swh
				HPG200/13
				(408) 447-5580
---------------------

robf2@pyuxf.UUCP (robert fair) (12/23/87)

In article <211@mosys.UUCP>, nortond@mosys.UUCP (Daniel A. Norton) writes:
> Copyright (c) 1987, Daniel A. Norton.  All Rights Reserved.
  decribes a bug in the intrinsic strlen() for MSC 5.0 using large
  model.

  Every intrinsic I have tried (strlen,strcpy,strncpy,memcpy...)
  has the same bug in large model (-AL -AH, -AC).

  Since these tend to produce horrible, subtle problems in the
  code I would suggest avoid using intrinsic functions *at all* in large
  model [Speaking from a very frustrating weekend tracking down such `bugs']
  and once again flame Microsoft for sending out a product which must be 
  almost totally untested !

  The loop optimization is also very buggy in large model, producing `hidden'
  bad code in inappropriate places.

  In short, the `optimizing' compiler really is:

      "An optimizing compiler for small & medium models and 
      a joke for large model"

  A very pissed off MSC 5.0 user  (and I LIKED MSC 3.0 & 4.0...)


Rob Fair
Currently at: Bellcore/CHC
ihnp4!pyuxww!pyuxf!robf2