Thread: Warning C6386: Buffer Overrun While Writing To 'p->op'

CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
CodeGuru Forums - A Developer.com Community for C++, C#, VB, Java and .NET Solutions
  • Register
  • Help
  • Remember Me?
  • Advanced Search
  • Home
  • Forum
  • Visual C++ & C++ Programming
  • C++ (Non Visual C++ Issues)
  • warning C6386: Buffer overrun while writing to 'p->op'
Results 1 to 7 of 7 Thread: warning C6386: Buffer overrun while writing to 'p->op'
  • Thread Tools
    • Show Printable Version
  • Display
    • Linear Mode
    • Switch to Hybrid Mode
    • Switch to Threaded Mode
  1. December 2nd, 2015, 03:14 PM #1 pebmeister
    • View Profile
    • View Forum Posts
    pebmeister is offline 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 Reply With Quote
  2. December 2nd, 2015, 03:20 PM #2 pebmeister
    • View Profile
    • View Forum Posts
    pebmeister is offline 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 Reply With Quote
  3. December 3rd, 2015, 03:21 AM #3 superbonzo
    • View Profile
    • View Forum Posts
    superbonzo is offline 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 Reply With Quote
  4. December 3rd, 2015, 09:59 AM #4 pebmeister
    • View Profile
    • View Forum Posts
    pebmeister is offline 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 Reply With Quote
  5. December 3rd, 2015, 10:52 AM #5 pebmeister
    • View Profile
    • View Forum Posts
    pebmeister is offline 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 Reply With Quote
  6. December 4th, 2015, 03:11 AM #6 superbonzo
    • View Profile
    • View Forum Posts
    superbonzo is offline Senior Member Join Date Oct 2008 Posts 1,456

    Re: warning C6386: Buffer overrun while writing to 'p->op'

    Quote Originally Posted by pebmeister View Post 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 Reply With Quote
  7. December 4th, 2015, 10:23 AM #7 pebmeister
    • View Profile
    • View Forum Posts
    pebmeister is offline 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 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
    1. Visual C++ Programming
    2. Visual C++ FAQs
    3. C++ (Non Visual C++ Issues)
    4. C++ and WinAPI
    5. Managed C++ and C++/CLI
    6. Visual C++ Bugs & Fixes
    7. Xamarin
    8. Graphics Programming
    9. Multithreading
    10. Network Programming
    11. Driver Development
  • C# Programming
    1. C-Sharp Programming
  • Visual Basic Programming
    1. Visual Basic 6.0 Programming
    2. Visual Basic .NET
    3. VBForums
  • Windows 8 and Later Store Development
    1. Modern Windows Apps (Metro)
  • Other .NET Programming
    1. ASP.NET
    2. .NET Framework
      1. .NET Installation and Configuration Issues
    3. ADO.NET
    4. Windows Presentation Foundation (WPF) & XAML forum
  • Java Programming
    1. Java Programming
  • Other Programming
    1. AJAX
    2. Scripting - Client Side
    3. Crystal Reports
    4. Database
    5. XML
    6. Wireless/Mobile Development
    7. Assembly
    8. Scripting - Server Side (PHP, Perl, etc.)
    9. SharePoint
    10. Python
      1. Python Articles
  • General Discussion
    1. General Developer Topics
    2. Project Planning, Design, and Management
    3. Testers and Testing
    4. Algorithms & Data Structures
    5. IoT, IoE, and Maker Forum (on VBForums)
    6. General Discussion / Chit Chat
    7. Announcements, Press Releases, & News
  • CodeGuru Community
    1. Feedback
    2. Articles Suggestions / Requests
    3. Programming Projects
      1. Game Engine Project
      2. C# Game(s) Project
      3. C++ Coding Project
      4. Project: Code War
    4. Testing Area
  • Slow Chat Archives
    1. eCamp Chat: Windows 8 for Developers
    2. Slow Chat: Talk with Microsoft Developer Teams
    3. Slow Chat: Developing Multithreaded Applications
    4. Slow Chat: C++0x
    5. Slow Chat: Visual C++: Yesterday, Today, and Tomorrow
  • Jobs
    1. Looking for Work
    2. Open Positions (Jobs)
  • CodeGuru Technical FAQs
    1. C++ FAQs
    2. STL FAQs
    3. Windows SDK FAQs
    4. Visual C++ FAQs
    5. MFC FAQs
    6. ATL FAQs
    7. .NET Framework (non-language specific) FAQs
    8. C# FAQs
    9. Visual Basic .NET FAQs
    10. Visual Basic FAQs
    11. CodeGuru Individual FAQs
    12. CodeGuru Individual Visual Basic FAQs
  • Retired Forum Areas
    1. Silverlight
    2. Directory Services
    3. General Windows and DNA Programming
    4. 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!
Terms of Service | About Us | Privacy Notice | Contact Us | Advertise | Sitemap| California - Do Not Sell My Info

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.

All times are GMT -5. The time now is 06:02 AM. Copyright TechnologyAdvice

Từ khóa » C C6386