none
GetBuffer(-1) Assert Error

    Question

  • Hi,

    When executing unit tests with GetBuffer(-1) in the code of the dll that wil be tested i get the next error:

    Assert.Fail failed. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.

    The code is:

    fpExport = fopen(csExportFile.GetBuffer(-1), "w+b");

    When i change the ...GetBuffer(-1) to ...GetBuffer() i get no error. Apparently -1 in GetBuffer was used before when the length of the CString was unknown. My project has 313 positions where GetBuffer(-1) is used, and i don't want to change the code right now. So is there another possebility to avoid the assert error, a project setting maybe?

    P.s. i use Visual Studio 2010 Premium.

    Regards,

    Programmer Leon

    Monday, April 16, 2012 8:19 AM

Answers

  • Hi Leon,

    Sorry for my delay.

    I try to run this test and get the same result. But I’m sure that it is related to your C++ class method, it is not a unit test issue.

    fpExport = fopen (csExportFile.GetBuffer(A), "w+b");

    It seems that if the int value A>=0 or use fpExport = fopen (csExportFile.GetBuffer(), "w+b")

    , the unit test will be passed. I’m not a C++ expert, but I did some research about this issue, it seems that “GetBuffer (XXX)”returns a pointer to the internal character buffer for the CString object. If possible, you could check this article:

    http://msdn.microsoft.com/en-us/library/kt26tkzx.aspx. I'm afraid that CSimpleStringT::GetBuffer() should not accept negative numbers for the length. We should throw an exception when negative lengths are passed in. This can hide potential buffer overrun problems.

    About this issue, if possible, you could try to use the GetBuffer() instead of the GetBuffer(-1), I know it is not a good solution, but you could submit this feedback to Microsoft Connect feedback portal: http://connect.microsoft.com, Microsoft engineers will evaluate them seriously. Thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us


    Friday, April 20, 2012 4:36 AM
    Moderator

All replies

  • Hi Leon,

    Thank you for posting in the MSDN forum.

    The code is:

    fpExport = fopen(csExportFile.GetBuffer(-1), "w+b");

    I’m not very sure this is related to your class method or this unit tests. Do you mean that this line code is in your unit test method? If so, did you change anything when the unit test is created automatically?

    Assert.Fail failed. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.

    Since it is hard for us to repro this issue, it would be better if you could share us a simple project. We will check it in our computer.

    Please attach your Visual Studio project, you can upload it to the sky driver, and then share the download link in your post.

    If there's any concern, please feel free to let us know.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 17, 2012 8:37 AM
    Moderator
  • Hi Jack,

    The ...GetBuffer(-1) code in not in the unit test method but in the application dll  that i'm testing.

    Best Regards,

    ProgrammerLeon

    Tuesday, April 17, 2012 10:25 AM
  • Hi Jack,

    I posted the project 'ExtendedMath_Unmanaged.zip' on sky driver. When you run the unit tests you will see that TestFibonacci2 will generate an error as i described in this thread.

    Best Regards,

    ProgrammerLeon

    Tuesday, April 17, 2012 11:51 AM
  • Hi Leon,

    Could you share us the download link in your post? So we could download and check it.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 18, 2012 2:03 AM
    Moderator
  • Hi Jack,

    The link is https://skydrive.live.com/#cid=2677F188C9EB54D8

    I hope that works, i'm not yet familiar with sky driver.

    Regards,

    ProgrammerLeon

    Wednesday, April 18, 2012 6:52 AM
  • Hi Leon,

    It seems that I couldn’t download it, but you could send your project to jackzhai at Hotmail dot com.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 18, 2012 9:12 AM
    Moderator
  • Hi Jack,

    I've send the project yesterday to you.

    Regards,

    ProgrammerLeon

    Thursday, April 19, 2012 9:17 AM
  • Hi Leon,

    Sorry for my delay.

    I try to run this test and get the same result. But I’m sure that it is related to your C++ class method, it is not a unit test issue.

    fpExport = fopen (csExportFile.GetBuffer(A), "w+b");

    It seems that if the int value A>=0 or use fpExport = fopen (csExportFile.GetBuffer(), "w+b")

    , the unit test will be passed. I’m not a C++ expert, but I did some research about this issue, it seems that “GetBuffer (XXX)”returns a pointer to the internal character buffer for the CString object. If possible, you could check this article:

    http://msdn.microsoft.com/en-us/library/kt26tkzx.aspx. I'm afraid that CSimpleStringT::GetBuffer() should not accept negative numbers for the length. We should throw an exception when negative lengths are passed in. This can hide potential buffer overrun problems.

    About this issue, if possible, you could try to use the GetBuffer() instead of the GetBuffer(-1), I know it is not a good solution, but you could submit this feedback to Microsoft Connect feedback portal: http://connect.microsoft.com, Microsoft engineers will evaluate them seriously. Thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us


    Friday, April 20, 2012 4:36 AM
    Moderator
  • Hi Jack,

    Thanks for the reply.

    GetBuffer() should also work, because if i read the article the default parameter for ReadBuffer(minimum length) is already -1, so ReadBuffer(-1) is not neccesary.

    Regards,

    ProgrammerLeon

    Friday, April 20, 2012 8:22 AM