KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q64791: Intrinsic Form of memcpy() Can Produce Incorrect Code

Article: Q64791
Product(s): See article
Version(s): 6.00   | 6.00
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | buglist6.00 | mspl13_c
Last Modified: 15-AUG-1990

In rare cases, the intrinsic form of memcpy may produce incorrect code
in the large memory model.

The code below, if compiled with the large memory model (/AL) and any
optimization that includes intrinsic functions (/Oi, /Ox, /Oz), will
cause the machine to hang under DOS and cause a protection violation
under OS/2.

To work around this problem, either compile without the /Oi option or
use #pragma "function(memcpy)" to specify the use of the function form
of memcpy(). The pragma may be turned off for functions following it
by using #pragma intrinsic(memcpy) to go back to the intrinsic form of
memcpy.

Sample Code
-----------

#include <stdio.h>
#include <string.h>
#include <malloc.h>

typedef struct
{
   int     dummy;
   char  *f_char[8];
   char  *f_attr[8];
} t_field;

void copy_fieldtxt(t_field *, t_field *, int, int);

void main(void)
{

   t_field *src, *dst;
   void    *calloc();

   src=(t_field *)calloc(1,sizeof(t_field));
   dst=(t_field *)calloc(1,sizeof(t_field));

   src->f_char[0]= strdup("Text string");
   src->f_attr[0]= strdup("attribute string");

   dst->f_char[1]=strdup("Text string");
   dst->f_attr[1]=strdup("attribute string");

   copy_fieldtxt(src,dst,0,1);
   printf("dst->attr[1]==>%s\n",dst->f_attr[1]);

}
/* Copies src->f_char[srcndx] to dst->f_char[dstndx]
    and src->f_attr[srcndx] to dst->f_attr[dstndx] */
void copy_fieldtxt(t_field *src, t_field *dst, int srcndx, int dstndx)
{
   int maxlen=strlen(dst->f_attr[dstndx]);

   memcpy(dst->f_char[dstndx], src->f_char[srcndx], maxlen);
   memcpy(dst->f_attr[dstndx], src->f_attr[srcndx],maxlen);
}

Microsoft has confirmed this to be a problem in C version 6.00. We are
researching this problem and will post new information here as it
becomes available.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.