Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Would you spend one week changing strcpy to lstrcpyn ?

Would you spend one week changing strcpy to lstrcpyn ?

Scheduled Pinned Locked Moved The Lounge
c++htmlcomquestioncareer
33 Posts 21 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jorgen Sigvardsson

    Yeah, but strcpy() isn't called by itself, is it? :) -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben.

    N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #20

    Maybe the implementation they use is recursive :eek:


    My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

    J 1 Reply Last reply
    0
    • G GuimaSun

      The newest idea from my company is a brute force approach to decrease Dr.Watson reports from our big C++ web application. Then, someone will have to change about 2000 strcpy to lstrcpyn passing a sizeof argument to make sure at least an overrun will not occur anymore. (believe it) Forget about using search and replace bots, it´s not safe. I think it could be made wasting 1 week of a BORED programmer and he would get a Tendinitis or something alike. If I were the choice, it ain't any better idea than look for a new job :( GuimaSun www.nexsun.com.br NEXSUN TechZone

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #21

      If strcpy() calls are generating crashes, it means your copies are overwriting the stack. This is a security risk. Unchecked strcpy() calls lead to buffer overflow attacks. If your boss doesn't want to fix numerous security holes, I would feel very worried if I were a user of that software. -- I'm Michael Dunn and I approve this post. Vote Trogdor in oh-four!

      M 1 Reply Last reply
      0
      • M Michael Dunn

        If strcpy() calls are generating crashes, it means your copies are overwriting the stack. This is a security risk. Unchecked strcpy() calls lead to buffer overflow attacks. If your boss doesn't want to fix numerous security holes, I would feel very worried if I were a user of that software. -- I'm Michael Dunn and I approve this post. Vote Trogdor in oh-four!

        M Offline
        M Offline
        m_mond
        wrote on last edited by
        #22

        Which is why you use strncpy... There's a very simple way to avoid basic stack overflows built in to the language. It still blows my mind that people still don't use the 'n' string and memory function. On the other hand, this is also a case for using string objects of some sort. That's something worth spending the time to fix, IMO.

        M 1 Reply Last reply
        0
        • T Todd Smith

          Write your own strcpy and check for bad writes. Todd Smith

          P Offline
          P Offline
          peterchen
          wrote on last edited by
          #23

          won't help with stack buffers.


          we are here to help each other get through this thing, whatever it is Vonnegut jr.
          boost your code || Fold With Us! || sighist | doxygen

          1 Reply Last reply
          0
          • G GuimaSun

            The newest idea from my company is a brute force approach to decrease Dr.Watson reports from our big C++ web application. Then, someone will have to change about 2000 strcpy to lstrcpyn passing a sizeof argument to make sure at least an overrun will not occur anymore. (believe it) Forget about using search and replace bots, it´s not safe. I think it could be made wasting 1 week of a BORED programmer and he would get a Tendinitis or something alike. If I were the choice, it ain't any better idea than look for a new job :( GuimaSun www.nexsun.com.br NEXSUN TechZone

            A Offline
            A Offline
            Alvaro Mendez
            wrote on last edited by
            #24

            I recommend a couple of things: 1. Don't use regular strncpy -- it has it's own problems. Use your own function, similar to this:

            char* STRNCPY(char* szTarget, const char* szSource, unsigned sizeOfTarget)
            {
            assert(szTarget);
            assert(szSource);
            assert(sizeOfTarget);

            strncpy(szTarget, szSource, sizeOfTarget - 1);
            szTarget[sizeOfTarget - 1] = 0;

            return szTarget;
            }

            2. Create a macro for copying into string buffers that calls the above function:

            #define STRCPY(szTarget, szSource) STRNCPY(szTarget, szSource, sizeof(szTarget))

            Then you can replace all cases where you have this type of code:

            char buffer[100];
            ....
            strcpy(buffer, someText);

            with

            char buffer[100];
            ....
            STRCPY(buffer, someText);

            This ensures you only copy no more than 99 (size - 1) characters into the buffer plus the null terminator. Regards, Alvaro


            Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we. - George W. Bush

            G 1 Reply Last reply
            0
            • M Member 96

              Nope, but then again I'm the boss so I would probably just make someone else do it if our organization were so brain damaged as to still be coding in anything but .net this century.


              84 keys are alright with me!

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #25

              So you honestly believe anyone not coding in .NET is brain damaged? Do you really have a view of the programming world or not? .NET is a great replacement for VB and it's nice for business/data driven apps. But, I wouldn't want a diver or a game (amongst other things) written in .NET. BTW, just throwing off responsibility for a screw up to someone who's not at fault just because you don't feel like doing it makes you a bad boss IMO. Also, the captain goes down with the ship, so if somebody is brain dead and screws up, it's ultimately your fault anyway because you're the boss. Jeremy Falcon

              A 1 Reply Last reply
              0
              • N Nemanja Trifunovic

                Maybe the implementation they use is recursive :eek:


                My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #26

                :laugh: That is certainly possible! :-D -- Weiter, weiter, ins verderben. Wir müssen leben bis wir sterben.

                1 Reply Last reply
                0
                • D Dale Thompson

                  GuimaSun wrote: the boss consider C++ slow and unsafe Oh god. Wonder what he thinks of C#? It sounds like you job isn't gaining you marketable experience anyhow - get the hell out!!! Dale Thompson

                  M Offline
                  M Offline
                  Matt Gerrans
                  wrote on last edited by
                  #27

                  Isn't it great to have a boss who once programmed 20 or 30 years ago and knows the one true path? Talk about premature optimization! Don't let him find out about how much the multitasking OS is cutting into the app's performance, or you'll be forced into writing your app in assembly as a BIOS extension. Matt Gerrans

                  1 Reply Last reply
                  0
                  • A Alvaro Mendez

                    I recommend a couple of things: 1. Don't use regular strncpy -- it has it's own problems. Use your own function, similar to this:

                    char* STRNCPY(char* szTarget, const char* szSource, unsigned sizeOfTarget)
                    {
                    assert(szTarget);
                    assert(szSource);
                    assert(sizeOfTarget);

                    strncpy(szTarget, szSource, sizeOfTarget - 1);
                    szTarget[sizeOfTarget - 1] = 0;

                    return szTarget;
                    }

                    2. Create a macro for copying into string buffers that calls the above function:

                    #define STRCPY(szTarget, szSource) STRNCPY(szTarget, szSource, sizeof(szTarget))

                    Then you can replace all cases where you have this type of code:

                    char buffer[100];
                    ....
                    strcpy(buffer, someText);

                    with

                    char buffer[100];
                    ....
                    STRCPY(buffer, someText);

                    This ensures you only copy no more than 99 (size - 1) characters into the buffer plus the null terminator. Regards, Alvaro


                    Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we. - George W. Bush

                    G Offline
                    G Offline
                    GuimaSun
                    wrote on last edited by
                    #28

                    Great. Definitively, as your assert suggests, more than 50% of c/c++ code worldwide are deployed on debug mode :-D Tx. GuimaSun www.nexsun.com.br NEXSUN TechZone

                    1 Reply Last reply
                    0
                    • M m_mond

                      Which is why you use strncpy... There's a very simple way to avoid basic stack overflows built in to the language. It still blows my mind that people still don't use the 'n' string and memory function. On the other hand, this is also a case for using string objects of some sort. That's something worth spending the time to fix, IMO.

                      M Offline
                      M Offline
                      Michael Dunn
                      wrote on last edited by
                      #29

                      Actually lstrcpyn() is better than strncpy() because lstrcpyn() always leaves the string null-terminated. With strncpy() you have to do that yourself if the source string length was >= destination buffer length. -- I'm Michael Dunn and I approve this post. Vote Trogdor in oh-four!

                      1 Reply Last reply
                      0
                      • G GuimaSun

                        The newest idea from my company is a brute force approach to decrease Dr.Watson reports from our big C++ web application. Then, someone will have to change about 2000 strcpy to lstrcpyn passing a sizeof argument to make sure at least an overrun will not occur anymore. (believe it) Forget about using search and replace bots, it´s not safe. I think it could be made wasting 1 week of a BORED programmer and he would get a Tendinitis or something alike. If I were the choice, it ain't any better idea than look for a new job :( GuimaSun www.nexsun.com.br NEXSUN TechZone

                        N Offline
                        N Offline
                        nfleming
                        wrote on last edited by
                        #30

                        Since you are on windows, install the CRT hooks, or just attach a debugger, set break on access vio, and see where it fires. strcpy, check the stack and see how you got there. Treat the cause, not the symptom. It is probably easier and more cost effective, and it is the right thing to do.

                        1 Reply Last reply
                        0
                        • J Jeremy Falcon

                          So you honestly believe anyone not coding in .NET is brain damaged? Do you really have a view of the programming world or not? .NET is a great replacement for VB and it's nice for business/data driven apps. But, I wouldn't want a diver or a game (amongst other things) written in .NET. BTW, just throwing off responsibility for a screw up to someone who's not at fault just because you don't feel like doing it makes you a bad boss IMO. Also, the captain goes down with the ship, so if somebody is brain dead and screws up, it's ultimately your fault anyway because you're the boss. Jeremy Falcon

                          A Offline
                          A Offline
                          afinnell
                          wrote on last edited by
                          #31

                          So you honestly believe anyone not coding in .NET is brain damaged? Do you really have a view of the programming world or not? .NET is a great replacement for VB and it's nice for business/data driven apps. But, I wouldn't want a diver or a game (amongst other things) written in .NET. I tend to agree with your statement but not your points you use to make up your statement. The same reasons where given when moving from Assembly to C, then again from C to C++. They are already developing games in C# and if .NET could be ran at the kernel level I'm sure people would develop drivers with it. So those points are not valid. That being said just assuming that a business has a choice to develop with .NET is another thing. Based on the shear number of strcpy's he has to replace it would appear it's a fairly mature application which means the decision for C++ was made a while ago. Rewriting an entire application in a different language without a business (a.k.a. customer) reason is a very bad mistake. Also I don't even think the original poster made a reference to the platform the application was written on. This could be Solaris for all we know. I do believe they should replace all the strcpy's with the parameterized version and be lashed so they never use the bad version again. - Drew

                          J 1 Reply Last reply
                          0
                          • G GuimaSun

                            I love std::string too, but the system was written in pure C (not C++ as I said sorry) and the boss consider C++ slow and unsafe :rolleyes: We could just rename .c to c++ and use extern "c" on WINAPI functions. GuimaSun www.nexsun.com.br NEXSUN TechZone

                            A Offline
                            A Offline
                            afinnell
                            wrote on last edited by
                            #32

                            the boss consider C++ slow and unsafe I've met people like this. There is very little hope in convincing them of doing the product correctly. You have a choice, put up or shut up. Depending on what needs to be done you need to convince upper management and the customer that they can get a better ROI by using a moderm language such as C#. Maintainability, and code reuse alone will save quite a bit of time and time equals money. - Drew

                            1 Reply Last reply
                            0
                            • A afinnell

                              So you honestly believe anyone not coding in .NET is brain damaged? Do you really have a view of the programming world or not? .NET is a great replacement for VB and it's nice for business/data driven apps. But, I wouldn't want a diver or a game (amongst other things) written in .NET. I tend to agree with your statement but not your points you use to make up your statement. The same reasons where given when moving from Assembly to C, then again from C to C++. They are already developing games in C# and if .NET could be ran at the kernel level I'm sure people would develop drivers with it. So those points are not valid. That being said just assuming that a business has a choice to develop with .NET is another thing. Based on the shear number of strcpy's he has to replace it would appear it's a fairly mature application which means the decision for C++ was made a while ago. Rewriting an entire application in a different language without a business (a.k.a. customer) reason is a very bad mistake. Also I don't even think the original poster made a reference to the platform the application was written on. This could be Solaris for all we know. I do believe they should replace all the strcpy's with the parameterized version and be lashed so they never use the bad version again. - Drew

                              J Offline
                              J Offline
                              Jeremy Falcon
                              wrote on last edited by
                              #33

                              Yeah, they have managed DirectX and from what I understand it's slower - just like every iteration of a Microsoft release. And, since you can't write drivers in .NET then my point about that was valid. You said so yourself. Jeremy Falcon

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups