- Forum
- Visual C++ & C++ Programming
- C++ (Non Visual C++ Issues)
- warning C6386: Buffer overrun while writing to 'p->op'
- If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register or Login before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
Results 1 to 7 of 7 Thread: warning C6386: Buffer overrun while writing to 'p->op' - December 2nd, 2015, 03:14 PM #1 pebmeister
- View Profile
- View Forum Posts
Member Join Date Oct 2015 Posts 26 warning C6386: Buffer overrun while writing to 'p->op' Hello I am using Visual Studio 2015 and ran Code Analysis on my solution It gives me this warning 1> Warning C6386 Buffer overrun while writing to 'p->op': the writable size is 'size' bytes, but '8' bytes might be written. pasm64-2015 c:\users\paul\desktop\paul_paul-pc_2148\pasm64\node.c 274 the line in question is p->op[i] = va_arg(ap, parseNodePtr); I don't see anything wrong with my code. I allocate nops * sizeof(parseNodePtr) bytes and iterate 1 to nops in my loop I also don't wan to use Code: #pragma warning(disable:6385) #pragma warning(disable:6386) This code is from an assembler that I wrote. The code runs perfectly on Windows, Linux, and MaC. I would like my code not to have any warnings (including Code Analysis) without using a bandaid. Any ideas? Code: /// <summary> /// create Operator parseNode /// </summary> /// <param name="oper">The operator.</param> /// <param name="nops">The number of operators.</param> /// <param name="">The operators.</param> /// <returns>parseNodePtr.</returns> parseNodePtr opr(int oper, int nops, ...) { va_list ap; int i = 0; size_t size = 0; const char* module = "opr"; /* allocate node */ parseNodePtr p = AllocateNode(); if (p == NULL) { FatalError(module, ErrorOutofMemory); return NULL; } size = nops * sizeof(parseNodePtr); if ((p->op = (parseNodePtr*) malloc(size)) == NULL) { FatalError(module, ErrorOutofMemory); return NULL; } /* copy information */ p->type = typeOpr; p->opr.oper = oper; p->nops = nops; va_start(ap, nops); for (i = 0; i < nops; i++) p->op[i] = va_arg(ap, parseNodePtr); va_end(ap); return p; } Reply With Quote - December 2nd, 2015, 03:20 PM #2 pebmeister
- View Profile
- View Forum Posts
Member Join Date Oct 2015 Posts 26 Re: warning C6386: Buffer overrun while writing to 'p->op' FYI here is the parseNode data type Code: typedef struct { int value; /* value of constant */ int IsPC; /* TRUE is value is PC */ } conParseNode; typedef struct { char* value; /* value of string */ char* allocated; /* allocated string */ int len; } strParseNode; /* identifiers */ typedef struct { char* name; SymbolTablePtr i; /* symbol entry */ } idParseNode; /* operators */ typedef struct { int oper; /* operator */ } oprParseNode; /* opcodes */ typedef struct { int instruction; /* untranslated instruction */ int opcode; /* opcode */ int mode; /* mode */ int pc; /* program counter */ } opParseNode; /* macro execution node */ typedef struct { void* macro; void* macroParams; } macParseNode; /* data definition node */ typedef struct { int size; /* 1 = byte 2 = word, 0 = string */ void* data; } dataParseNode; /* nodes union */ typedef struct parseNode { nodeEnum type; /* type of node */ union { conParseNode con; /* constants */ idParseNode id; /* identifiers */ oprParseNode opr; /* operators */ opParseNode opcode; /* opcodes */ macParseNode macro; /* macro execution */ dataParseNode data; /* numeric data node */ strParseNode str; /* string node */ }; int nops; /* number of operands */ struct parseNode **op; /* operands */ struct parseNode* next; /* next node in tree */ struct parseNode* prev; /* previous node */ } parseNode, *parseNodePtr; Reply With Quote - December 3rd, 2015, 03:21 AM #3 superbonzo
- View Profile
- View Forum Posts
Senior Member Join Date Oct 2008 Posts 1,456 Re: warning C6386: Buffer overrun while writing to 'p->op' as far as I can tell, I see three problems: 1-if nops is negative you may end up allocating a lot of unused memory 2-if nops is zero it may cause a "FatalError" or maybe not, it's implementation defined 3-if the nops * sizeof product overflows you may end up with a buffer overrun indeed ( on a 32bit system, take nops = 2^30, you'll have a zero 'size' malloc (see point 2) ) maybe, the code analisys tool is complaining about point 3 ... Reply With Quote - December 3rd, 2015, 09:59 AM #4 pebmeister
- View Profile
- View Forum Posts
Member Join Date Oct 2015 Posts 26 Re: warning C6386: Buffer overrun while writing to 'p->op' Thanks for the response. I can now see how it could be a buffer overrun. In the practical world nops will always be 0,1 or 2 I should special case 0. I will also do a range check on nops the entry. Reply With Quote - December 3rd, 2015, 10:52 AM #5 pebmeister
- View Profile
- View Forum Posts
Member Join Date Oct 2015 Posts 26 Re: warning C6386: Buffer overrun while writing to 'p->op' Wow found the real problem [code] size = nops * sizeof(parseNodePtr); [code] should be [code] size = nops * sizeof(parseNode); [code] Reply With Quote - December 4th, 2015, 03:11 AM #6 superbonzo
- View Profile
- View Forum Posts
Senior Member Join Date Oct 2008 Posts 1,456 Re: warning C6386: Buffer overrun while writing to 'p->op' Originally Posted by pebmeister Wow found the real problem I doubt so, unless your real intent was to allocate memory for nops parseNode's for constructing nops parseNode pointers ... does not make much sense to me Reply With Quote - December 4th, 2015, 10:23 AM #7 pebmeister
- View Profile
- View Forum Posts
Member Join Date Oct 2015 Posts 26 Re: warning C6386: Buffer overrun while writing to 'p->op' I changed it slightly and now I get no warnings. Code: /// <summary> /// create Operator parnode /// </summary> /// <param name="oper">The operator.</param> /// <param name="nops">The number of ops.</param> /// <param name="">The .</param> /// <returns>parseNodePtr.</returns> parseNodePtr opr(int oper, int nops, ...) { va_list ap; int i = 0; const char* module = "opr"; /* allocate node */ parseNodePtr p = AllocateNode(); if (p == NULL) { FatalError(module, ErrorOutofMemory); return NULL; } /* copy information */ p->type = typeOpr; p->opr.oper = oper; if (nops < 0 || nops >= MAX_OPS) { FatalError(module, ErrorValueOutofRange); return NULL; } p->nops = nops; if (nops > 0 && nops < MAX_OPS) { p->op = (parseNodePtr*) malloc(sizeof(parseNodePtr) * nops); if ((p->op) == NULL) { FatalError(module, ErrorOutofMemory); return NULL; } va_start(ap, nops); for (i = 0; i < nops; i++) p->op[i] = va_arg(ap, parseNodePtr); va_end(ap); } return p; } Reply With Quote Quick Navigation C++ (Non Visual C++ Issues) Top - Site Areas
- Settings
- Private Messages
- Subscriptions
- Who's Online
- Search Forums
- Forums Home
- Forums
- Visual C++ & C++ Programming
- Visual C++ Programming
- Visual C++ FAQs
- C++ (Non Visual C++ Issues)
- C++ and WinAPI
- Managed C++ and C++/CLI
- Visual C++ Bugs & Fixes
- Xamarin
- Graphics Programming
- Multithreading
- Network Programming
- Driver Development
- C# Programming
- C-Sharp Programming
- Visual Basic Programming
- Visual Basic 6.0 Programming
- Visual Basic .NET
- VBForums
- Windows 8 and Later Store Development
- Modern Windows Apps (Metro)
- Other .NET Programming
- ASP.NET
- .NET Framework
- .NET Installation and Configuration Issues
- ADO.NET
- Windows Presentation Foundation (WPF) & XAML forum
- Java Programming
- Java Programming
- Other Programming
- AJAX
- Scripting - Client Side
- Crystal Reports
- Database
- XML
- Wireless/Mobile Development
- Assembly
- Scripting - Server Side (PHP, Perl, etc.)
- SharePoint
- Python
- Python Articles
- General Discussion
- General Developer Topics
- Project Planning, Design, and Management
- Testers and Testing
- Algorithms & Data Structures
- IoT, IoE, and Maker Forum (on VBForums)
- General Discussion / Chit Chat
- Announcements, Press Releases, & News
- CodeGuru Community
- Feedback
- Articles Suggestions / Requests
- Programming Projects
- Game Engine Project
- C# Game(s) Project
- C++ Coding Project
- Project: Code War
- Testing Area
- Slow Chat Archives
- eCamp Chat: Windows 8 for Developers
- Slow Chat: Talk with Microsoft Developer Teams
- Slow Chat: Developing Multithreaded Applications
- Slow Chat: C++0x
- Slow Chat: Visual C++: Yesterday, Today, and Tomorrow
- Jobs
- Looking for Work
- Open Positions (Jobs)
- CodeGuru Technical FAQs
- C++ FAQs
- STL FAQs
- Windows SDK FAQs
- Visual C++ FAQs
- MFC FAQs
- ATL FAQs
- .NET Framework (non-language specific) FAQs
- C# FAQs
- Visual Basic .NET FAQs
- Visual Basic FAQs
- CodeGuru Individual FAQs
- CodeGuru Individual Visual Basic FAQs
- Retired Forum Areas
- Silverlight
- Directory Services
- General Windows and DNA Programming
- Windows OS Issues
« Previous Thread | Next Thread » Posting Permissions - You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
- BB code is On
- Smilies are On
- [IMG] code is On
- [VIDEO] code is On
- HTML code is Off
Forum Rules -- Blue Codeguru -- Default Mobile Style ---- Child of Default Mobile Style | Click Here to Expand Forum to Full Width Featured * The Best Reasons to Target Windows 8 Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform. - * Porting from Android to Windows 8: The Real Story Do you have an Android application? How hard would it really be to port to Windows 8?
- * Guide to Porting Android Applications to Windows 8 If you've already built for Android, learn what do you really need to know to port your application to Windows Phone 8.
- * HTML5 Development Center Our portal for articles, videos, and news on HTML5, CSS3, and JavaScript
- * Windows App Gallery See the Windows 8.x apps we've spotlighted or submit your own app to the gallery!
|