1.1 ÒýÑÔ
¶ÔÓÚÈκÎʹÓà C ÓïÑÔµÄÈË£¬Èç¹ûÎÊËûÃÇ C ÓïÑÔµÄ×î´ó·³ÄÕÊÇʲô£¬ÆäÖÐÐí¶àÈË¿ÉÄÜ»á»Ø´ð˵ÊÇÖ¸ÕëºÍÄÚ´æй©¡£ÕâЩµÄÈ·ÊÇÏûºÄÁË¿ª·¢ÈËÔ±´ó¶àÊýµ÷ÊÔʱ¼äµÄÊÂÏî¡£Ö¸ÕëºÍÄÚ´æ驶ÔijЩ¿ª·¢ÈËÔ±À´ËµËƺõÁîÈËη¾å£¬µ«ÊÇÒ»µ©ÄúÁ˽âÁËÖ¸Õë¼°Æä¹ØÁªÄÚ´æ²Ù×÷µÄ»ù´¡£¬ËüÃǾÍÊÇÄúÔÚ C ÓïÑÔÖÐÓµÓеÄ×îÇ¿´ó¹¤¾ß¡£
±¾ÎĽ«ÓëÄú·ÖÏí¿ª·¢ÈËÔ±ÔÚ¿ªÊ¼Ê¹ÓÃÖ¸ÕëÀ´±à³ÌÇ°Ó¦¸ÃÖªµÀµÄÃØÃÜ¡£
±¾ÎÄÄÚÈÝ°üÀ¨£º
¡ñ µ¼ÖÂÄÚ´æÆÆ»µµÄÖ¸Õë²Ù×÷ÀàÐÍ
¡ñ ÔÚʹÓö¯Ì¬ÄÚ´æ·ÖÅäʱ±ØÐ뿼Âǵļì²éµã
¡ñ µ¼ÖÂÄÚ´æй©µÄ³¡¾°
Èç¹ûÄúÔ¤ÏÈÖªµÀʲôµØ·½¿ÉÄܳö´í£¬ÄÇôÄú¾ÍÄܹ»Ð¡ÐıÜÃâÏÝÚ壬²¢Ïû³ý´ó¶àÊýÓëÖ¸ÕëºÍÄÚ´æÏà¹ØµÄÎÊÌâ¡£
1.2 ʲôµØ·½¿ÉÄܳö´í£¿
Óм¸ÖÖÎÊÌⳡ¾°¿ÉÄÜ»á³öÏÖ£¬´Ó¶ø¿ÉÄÜÔÚÍê³ÉÉú³Éºóµ¼ÖÂÎÊÌâ¡£ÔÚ´¦ÀíÖ¸Õëʱ£¬Äú¿ÉÒÔʹÓñ¾ÎÄÖеÄÐÅÏ¢À´±ÜÃâÐí¶àÎÊÌâ¡£
1.2.1 δ³õʼ»¯µÄÄÚ´æ
ÔÚ±¾ÀýÖУ¬pÒѱ»·ÖÅäÁË 10 ¸ö×Ö½Ú¡£Õâ 10 ¸ö×Ö½Ú¿ÉÄÜ°üº¬À¬»øÊý¾Ý£¬char *p = (char *)malloc ( 10 );
Èç¹ûÔÚ¶ÔÕâ¸öp¸³ÖµÇ°£¬Ä³¸ö´úÂë¶Î³¢ÊÔ·ÃÎÊËü£¬Ôò¿ÉÄÜ»á»ñµÃÀ¬»øÖµ£¬ÄúµÄ³ÌÐò¿ÉÄܾßÓв»¿ÉÔ¤²âµÄÐÐΪ¡£p¿ÉÄܾßÓÐÄúµÄ³ÌÐò´ÓδÔøÔ¤Áϵ½µÄÖµ,Á¼ºÃµÄʵ¼ùÊÇʼÖÕ½áºÏʹÓÃmemsetºÍmalloc£¬»òÕßʹÓÃcalloc¡£
char *p = (char *)malloc (10);memset(p,¡¯ ¡¯,10);
ÏÖÔÚ£¬¼´Ê¹Í¬Ò»¸ö´úÂë¶Î³¢ÊÔÔÚ¶Ôp¸³ÖµÇ°·ÃÎÊËü£¬¸Ã´úÂë¶ÎÒ²ÄÜÕýÈ·´¦ÀíNullÖµ£¨ÔÚÀíÏëÇé¿öÏÂÓ¦¾ßÓеÄÖµ£©£¬È»ºó½«¾ßÓÐÕýÈ·µÄÐÐΪ¡£
1.2.2 Äڴ渲¸Ç
ÓÉÓÚpÒѱ»·ÖÅäÁË 10 ¸ö×Ö½Ú£¬Èç¹ûij¸ö´úÂëƬ¶Î³¢ÊÔÏòpдÈëÒ»¸ö 11 ×Ö½ÚµÄÖµ£¬Ôò¸Ã²Ù×÷½«ÔÚ²»¸æËßÄúµÄÇé¿öÏÂ×Ô¶¯´ÓÆäËûij¸öλÖá°³Ôµô¡±Ò»¸ö×Ö½Ú¡£ÈÃÎÒÃǼÙÉèÖ¸Õëq±íʾ¸ÃÄÚ´æ¡£½øÐи²¸ÇÖ®ºó£¬½á¹û£¬Ö¸Õëq½«¾ßÓдÓδԤÁϵ½µÄÄÚÈÝ¡£¼´Ê¹ÄúµÄÄ£¿é±àÂëµÃ×ã¹»ºÃ£¬Ò²¿ÉÄÜÓÉÓÚij¸ö¹²´æÄ£¿éÖ´ÐÐijЩÄÚ´æ²Ù×÷¶ø¾ßÓв»ÕýÈ·µÄÐÐΪ¡£ÏÂÃæµÄʾÀý´úÂëƬ¶ÎÒ²¿ÉÒÔ˵Ã÷ÕâÖÖ³¡¾°¡£
char *name = (char *) malloc(11);
// Assign some value to name
memcpy ( p,name,11); // Problem begins here
ÔÚ±¾ÀýÖУ¬memcpy²Ù×÷³¢ÊÔ½« 11 ¸ö×Ö½Úдµ½p£¬¶øºóÕß½ö±»·ÖÅäÁË 10 ¸ö×Ö½Ú¡£
×÷ΪÁ¼ºÃµÄʵ¼ù£¬Ã¿µ±ÏòÖ¸ÕëдÈëֵʱ£¬¶¼ÒªÈ·±£¶Ô¿ÉÓÃ×Ö½ÚÊýºÍËùдÈëµÄ×Ö½ÚÊý½øÐн»²æºË¶Ô¡£Ò»°ãÇé¿öÏ£¬memcpyº¯Êý½«ÊÇÓÃÓÚ´ËÄ¿µÄµÄ¼ì²éµã¡£
1.2.3 ÄÚ´æ¶ÁÈ¡Ô½½ç
ÄÚ´æ¶ÁÈ¡Ô½½ç (overread) ÊÇÖ¸Ëù¶ÁÈ¡µÄ×Ö½ÚÊý¶àÓÚËüÃÇÓ¦ÓеÄ×Ö½ÚÊý¡£Õâ¸öÎÊÌâ²¢²»Ì«ÑÏÖØ£¬Ôڴ˾Ͳ»ÔÙÏêÊöÁË¡£ÏÂÃæµÄ´úÂëÌṩÁËÒ»¸öʾÀý¡£
char *ptr = (char *)malloc(10);
char name[20] ; memcpy ( name,ptr,20); // Problem begins here
ÔÚ±¾ÀýÖУ¬memcpy²Ù×÷³¢ÊÔ´Óptr¶ÁÈ¡ 20 ¸ö×Ö½Ú£¬µ«ÊǺóÕß½ö±»·ÖÅäÁË 10 ¸ö×Ö½Ú¡£Õ⻹»áµ¼Ö²»Ï£ÍûµÄÊä³ö¡£
1.3 ÄÚ´æй©
ÄÚ´æй©¿ÉÄÜÕæÕýÁîÈËÌÖÑá¡£ÏÂÃæµÄÁбíÃèÊöÁËһЩµ¼ÖÂÄÚ´æй©µÄ³¡¾°¡£
¡ñ ÖØи³ÖµÎÒ½«Ê¹ÓÃÒ»¸öʾÀýÀ´ËµÃ÷ÖØи³ÖµÎÊÌâ¡£
char *memoryArea = malloc(10);
char *newArea = malloc(10);
memoryAreaºÍnewArea·Ö±ð±»·ÖÅäÁË 10 ¸ö×Ö½Ú£¬¡£Èç¹ûijÈËÖ´ÐÐÈçÏÂËùʾµÄÓï¾ä£¨Ö¸ÕëÖØи³Öµ£©¡¡
memoryArea = newArea;
ÔòËü¿Ï¶¨»áÔÚ¸ÃÄ£¿é¿ª·¢µÄºóÐø½×¶Î¸øÄú´øÀ´Âé·³¡£
ÔÚÉÏÃæµÄ´úÂëÓï¾äÖУ¬¿ª·¢ÈËÔ±½«memoryAreaÖ¸Õ븳ֵ¸ønewAreaÖ¸Õë¡£½á¹û£¬memoryAreaÒÔÇ°ËùÖ¸ÏòµÄÄÚ´æλÖñä³ÉÁ˹ÂÁ¢µÄ£¬ÈçÏÂÃæµÄͼ 5Ëùʾ¡£ËüÎÞ·¨ÊÍ·Å£¬ÒòΪûÓÐÖ¸Ïò¸ÃλÖõÄÒýÓá£Õâ»áµ¼Ö 10 ¸ö×Ö½ÚµÄÄÚ´æй©¡£
¡ñ ÔÚ¶ÔÖ¸Õ븳ֵǰ£¬ÇëÈ·±£ÄÚ´æλÖò»»á±äΪ¹ÂÁ¢µÄ¡£
¡ñ Ê×ÏÈÊͷŸ¸¿é¼ÙÉèÓÐÒ»¸öÖ¸ÕëmemoryArea£¬ËüÖ¸ÏòÒ»¸ö 10 ×Ö½ÚµÄÄÚ´æλÖ᣸ÃÄÚ´æλÖõĵÚÈý¸ö×Ö½ÚÓÖÖ¸Ïòij¸ö¶¯Ì¬·ÖÅäµÄ 10 ×Ö½ÚµÄÄÚ´æλÖã¬
free(memoryArea)
Èç¹ûͨ¹ýµ÷Óà free À´ÊÍ·ÅÁËmemoryArea£¬ÔònewAreaÖ¸ÕëÒ²»áÒò´Ë¶ø±äµÃÎÞЧ¡£newAreaÒÔÇ°ËùÖ¸ÏòµÄÄÚ´æλÖÃÎÞ·¨ÊÍ·Å£¬ÒòΪÒѾûÓÐÖ¸Ïò¸ÃλÖõÄÖ¸Õë¡£»»¾ä»°Ëµ£¬newAreaËùÖ¸ÏòµÄÄÚ´æλÖñäΪÁ˹ÂÁ¢µÄ£¬´Ó¶øµ¼ÖÂÁËÄÚ´æй©¡£
ÿµ±ÊͷŽṹ»¯µÄÔªËØ£¬¶ø¸ÃÔªËØÓÖ°üº¬Ö¸Ïò¶¯Ì¬·ÖÅäµÄÄÚ´æλÖõÄÖ¸Õëʱ£¬Ó¦Ê×ÏȱéÀú×ÓÄÚ´æλÖã¨ÔÚ´ËÀýÖÐΪnewArea£©£¬²¢´ÓÄÇÀ↑ʼÊÍ·Å£¬È»ºóÔÙ±éÀú»Ø¸¸½Úµã¡£
ÕâÀïµÄÕýȷʵÏÖÓ¦¸ÃΪ£º
free( memoryArea->newArea);
free(memoryArea);
1.4 ·µ»ØÖµµÄ²»ÕýÈ·´¦Àí
ÓÐʱ£¬Ä³Ð©º¯Êý»á·µ»Ø¶Ô¶¯Ì¬·ÖÅäµÄÄÚ´æµÄÒýÓ᣸ú×Ù¸ÃÄÚ´æλÖò¢ÕýÈ·µØ´¦ÀíËü¾Í³ÉΪÁËcallingº¯ÊýµÄÖ°Ôð¡£
char *func ( )
{
return malloc(20); // make sure to memset this location to ¡®¡¯¡
}
void callingFunc ( )
{
func ( ); // Problem lies here
}
ÔÚÉÏÃæµÄʾÀýÖУ¬callingFunc()º¯ÊýÖжÔfunc()º¯ÊýµÄµ÷ÓÃδ´¦Àí¸ÃÄÚ´æλÖõķµ»ØµØÖ·¡£½á¹û£¬func()º¯ÊýËù·ÖÅäµÄ 20 ¸ö×ֽڵĿé¾Í¶ªÊ§ÁË£¬²¢µ¼ÖÂÁËÄÚ´æй©¡£
1.5 ¹é»¹ÄúËù»ñµÃµÄ
ÔÚ¿ª·¢×é¼þʱ£¬¿ÉÄÜ´æÔÚ´óÁ¿µÄ¶¯Ì¬ÄÚ´æ·ÖÅä¡£Äú¿ÉÄÜ»áÍüÁ˸ú×ÙËùÓÐÖ¸Õ루ָÏòÕâЩÄÚ´æλÖã©£¬²¢ÇÒijЩÄÚ´æ¶ÎûÓÐÊÍ·Å£¬»¹±£³Ö·ÖÅä¸ø¸Ã³ÌÐò¡£Ê¼ÖÕÒª¸ú×ÙËùÓÐÄÚ´æ·ÖÅ䣬²¢ÔÚÈκÎÊʵ±µÄʱºòÊÍ·ÅËüÃÇ¡£ÊÂʵÉÏ£¬¿ÉÒÔ¿ª·¢Ä³ÖÖ»úÖÆÀ´¸ú×ÙÕâЩ·ÖÅ䣬±ÈÈçÔÚÁ´±í½Úµã±¾ÉíÖб£ÁôÒ»¸ö¼ÆÊýÆ÷£¨µ«Äú»¹±ØÐ뿼ÂǸûúÖƵĶîÍ⿪Ïú£©¡£
1.6 ·ÃÎÊ¿ÕÖ¸Õë
·ÃÎÊ¿ÕÖ¸ÕëÊǷdz£Î£Ïյģ¬ÒòΪËü¿ÉÄÜʹÄúµÄ³ÌÐò±ÀÀ£¡£Ê¼ÖÕҪȷ±£Äú²»ÊÇÔÚ·ÃÎÊ¿ÕÖ¸Õë¡£