各位大神們:
我在網上查到的
CString str = "Some Data";
str.Format("%s%d", str, 123); //Attention: str is also used in the parameter list.
這樣寫可能會導致buffer too small的問題
現在有以下問題:
(1)以下句子會導致buffer too small的問題嗎:
DWORD ret = GetPrivateProfileString( "PARA", "train", "", testString.GetBuffer(MAX_PATH), MAX_PATH ,"E:\\test.ini");
testString.Format("%s",testString.GetBuffer());
(2)
“f:\dd\vctools\crt_bld\self_x86\crt\src\vsprintf.c Line:244”
Expression:("Buffer too small", 0).
這個問題是針對format的報錯嗎?還是其他問題也會導致這個錯誤?我在網上查到的其他串處理如果出錯也會報buffer too small,但是位置不在Line:244
uj5u.com熱心網友回復:
急,在線等
uj5u.com熱心網友回復:
不要迷信書、考題、老師、回帖;要迷信CPU、編譯器、除錯器、運行結果。
并請結合“盲人摸太陽”和“駕船出海時一定只帶一個指南針。”加以理解。
任何理論、權威、傳說、真理、標準、解釋、想象、知識……都比不上擺在眼前的事實!
代碼功能歸根結底不是別人幫自己看或講解或注釋出來的;而是被自己靜下心來花足夠長的時間和精力親自動手單步或設斷點或對執行到某步獲得的中間結果顯示或寫到日志檔案中一步一步分析出來的。
提醒:再牛×的老師也無法代替學生自己領悟和上廁所!
單步除錯和設斷點除錯(VS IDE中編譯連接通過以后,按F10或F11鍵單步執行,按Shift+F11退出當前函式;在某行按F9設斷點后按F5執行停在該斷點處。)是程式員必須掌握的技能之一。
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。
uj5u.com熱心網友回復:
CString str = TEXT("Some Data");str.AppendFormat(TEXT("%d"), 123);
DWORD ret = GetPrivateProfileString( TEXT("PARA"), TEXT("train"), TEXT(""), testString.GetBuffer(MAX_PATH), MAX_PATH ,TEXT("E:\\test.ini"));
testString.RelaseBuffer();
uj5u.com熱心網友回復:
因為這個問題才出現過一次,彈出了buffer too small的問題,現在重現不了這個問題,沒法用CallStack除錯,想請教一下是否能確認那兩句陳述句代碼有問題呢?
uj5u.com熱心網友回復:
CString str;
CString teststring= _T("1234");
str.Format("%s",teststring);
Format用%s有沒有安全隱患?
uj5u.com熱心網友回復:
CString::Formatvoid Format( LPCTSTR lpszFormat, ... );
void Format( UINT nFormatID, ... );
Parameters
lpszFormat
A format-control string.
nFormatID
The string resource identifier that contains the format-control string.
Remarks
Call this member function to write formatted data to a CString in the same way that sprintf formats data into a C-style character array. This function formats and stores a series of characters and values in the CString. Each optional argument (if any) is converted and output according to the corresponding format specification in lpszFormat or from the string resource identified by nFormatID.
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
will cause unpredictable results.
When you pass a character string as an optional argument, you must cast it explicitly as LPCTSTR. The format has the same form and function as the format argument for the printf function. (For a description of the format and arguments, seeprintf in the Run-Time Library Reference.) A null character is appended to the end of the characters written.
For more information, seesprintf in the Run-Time Library Reference.
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
// 注意str 同時被用作引數
uj5u.com熱心網友回復:
所以 不要 這樣用testString.Format("%s",testString.GetBuffer());
CString tmp=testString; testString.Empty();
testString.Format("%s", tmp);
uj5u.com熱心網友回復:
testString.Format("%s",testString.GetBuffer()); 這個句子在一個產品里用了8年了都沒有出問題,現在出了一個buffer too small的問題,所以一直懷疑又不敢確定啊
uj5u.com熱心網友回復:
CString,不能自己做運算再賦給自己,我以前也遇到過。
uj5u.com熱心網友回復:
建議用一個中間變數緩沖一下, 如果函式的引數中沒有考慮輸入輸出相同時,很容易出問題CString strTmp(str);
str.Format(_T("%s"), (LPCTSTR)strTmp);
uj5u.com熱心網友回復:
CString str1 = "Some Data",str;
str.Format("%s%d", str, 123);
這樣使用才是安全的
uj5u.com熱心網友回復:
CString str1 = "Some Data",str;
str.Format("%s%d", str1, 123);
糾正一下上樓的錯誤
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/88176.html
標籤:基礎類
