Buffer Overrun
-
I have a buffer overrun problem that i am not 100% sure of the best way to fix it. If someone can give me some direction i would appreciate it. I have the following : Two structs with fields of different values that i am attempting to StrCpy(); typedef struct _myInfo { TCHAR szOwnerLastName[64]; TCHAR szOwnerFirstName[64]; } MyInfo; typedef struct _otherInfo { TCHAR szOwnerLastName[32]; TCHAR szOwnerFirstName[32]; } OtherInfo; MyInfo* pMyInfo; OtherInfo* pOtherInfo; After populating these fields, here is what i find: If i do a : StrCpy(pOtherInfo->szOwnerLastName, pMyInfo->szOwnerLastName); Since MyInfo is bigger than OtherInfo, the above statement basically writes into the OtherInfo's Firstname field. Is there a nice way to get around this? I cannot change the field sizes for either structure. Thanks, cw
-
I have a buffer overrun problem that i am not 100% sure of the best way to fix it. If someone can give me some direction i would appreciate it. I have the following : Two structs with fields of different values that i am attempting to StrCpy(); typedef struct _myInfo { TCHAR szOwnerLastName[64]; TCHAR szOwnerFirstName[64]; } MyInfo; typedef struct _otherInfo { TCHAR szOwnerLastName[32]; TCHAR szOwnerFirstName[32]; } OtherInfo; MyInfo* pMyInfo; OtherInfo* pOtherInfo; After populating these fields, here is what i find: If i do a : StrCpy(pOtherInfo->szOwnerLastName, pMyInfo->szOwnerLastName); Since MyInfo is bigger than OtherInfo, the above statement basically writes into the OtherInfo's Firstname field. Is there a nice way to get around this? I cannot change the field sizes for either structure. Thanks, cw
LCI wrote:
Is there a nice way to get around this?
Yes, use
strncpy()
instead. Or usestd::string
and not worry about it.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I have a buffer overrun problem that i am not 100% sure of the best way to fix it. If someone can give me some direction i would appreciate it. I have the following : Two structs with fields of different values that i am attempting to StrCpy(); typedef struct _myInfo { TCHAR szOwnerLastName[64]; TCHAR szOwnerFirstName[64]; } MyInfo; typedef struct _otherInfo { TCHAR szOwnerLastName[32]; TCHAR szOwnerFirstName[32]; } OtherInfo; MyInfo* pMyInfo; OtherInfo* pOtherInfo; After populating these fields, here is what i find: If i do a : StrCpy(pOtherInfo->szOwnerLastName, pMyInfo->szOwnerLastName); Since MyInfo is bigger than OtherInfo, the above statement basically writes into the OtherInfo's Firstname field. Is there a nice way to get around this? I cannot change the field sizes for either structure. Thanks, cw
Do you have to use raw strings? Why not use
std::string
or some other string class? What should happen to the bit of the string that can't fit in the smaller buffer?Steve