C6059

تحذير C6059: معلمة غير صحيحة للطول في يتصل إلى <دالة>. تمرير عدد حرف/ حروف المتبقية، غير الحجم احتياطي <متغير>

Th هو التحذير يشير إلى أن استدعاء دالة سلسلة سلسلة هو من المحتمل أن يكون تمرير القيمة غير صحيحة لعدد حرف/ حروف لسلسلة. قد يتسبب هذا عيب بتجاوز سعة احتياطي exploitable أو التعطل الكلي. سبب شائع لترتيب هو defect هو تمرير الحجم احتياطي، بدلاً من عدد حرف/ حروف في احتياطي، إلى وظيفة معالجة سلسلة المتبقية.

مثال

يلي تعليمات برمجية ينشئ هذا التحذير:

#include <string.h>
#define MAX 25

void f( )
{
  char szTarget[MAX];
  char *szState ="Washington";
  char *szCity="Redmond, ";

  strncpy(szTarget,szCity, MAX);
  szTarget[MAX -1] = '\0';
  strncat(szTarget, szState, MAX); //wrong size 
  // code ...                                 
}

لتصحيح هذا التحذير، قم باستخدام العدد الصحيح للأحرف لسلسلة كما هو موضح في التالية تعليمات برمجية:

#include <string.h>
#define MAX 25

void f( )
{
  char szTarget[MAX];
  char *szState ="Washington";
  char *szCity="Redmond, ";

  strncpy(szTarget,szCity, MAX);
  szTarget[MAX -1] = '\0';
  strncat(szTarget, szState, MAX - strlen(szTarget)); // correct size 
  // code ...                                 
}

إلى تصحيح هذا التحذير باستخدام دالة معالجة سلسلة آمن، راجع تعليمات برمجية التالية:

#include <string.h>

void f( )
{
  char *szState ="Washington";
  char *szCity="Redmond, ";

  size_t nTargetSize = strlen(szState) + strlen(szCity) + 1;
  char *szTarget= new char[nTargetSize];

  strncpy_s(szTarget, nTargetSize, szCity,strlen(szCity));
  strncat_s(szTarget, nTargetSize, szState,
                    nTargetSize - strlen(szTarget));
  // code ...
  delete [] szTarget;
}

راجع أيضًا:

المرجع

strncpy_s _strncpy_s_l ، wcsncpy_s ، _wcsncpy_s_l ، _mbsncpy_s ، _mbsncpy_s_l

strncat_s _strncat_s_l ، wcsncat_s ، _wcsncat_s_l ، _mbsncat_s ، _mbsncat_s_l