DiscussionsIssue archiveOCCT:Foundation Classes

Archived issue #0029344

Foundation Classes, TCollection_AsciiString - replace confusing strncpy with memcpy

Open CASCADEOCCT:Foundation Classesclosed11 public notes

Search issues

Description

TCollection_AsciiString implementation uses strncpy + strlen combination which does not make sense, because after strlen the length of the string is already determined and plain memcpy can be called instead.

Steps to reproduce

N/A

Public activity

11 archived notes

Participants are labeled by their role within this record.

01Commenter 2
Branch [archived branch] has been created by Author.

[revision removed]


Detailed log of new commits:

Author: Author
Date: Fri Nov 24 18:37:01 2017 +0300

    0029344: Foundation Classes, TCollection_AsciiString - replace confusing strncpy with memcpy
    
    strncpy usage within TCollection_AsciiString has been replaced by memcpy
    where string lenght has been already determined.
    
    TCollection_AsciiString(const char* , int ) and TCollection_AsciiString::SetValue()
    have been modified to throw exception on attempt to define invalid length of the string.
02Commenter 2
Branch [archived branch] has been updated by Author.

[revision removed]


Detailed log of new commits:

Author: Author
Date: Fri Nov 24 22:00:08 2017 +0300

    # fix regressions

03Commenter 2
Branch [archived branch] has been created by Author.

[revision removed]


Detailed log of new commits:

Author: Author
Date: Fri Nov 24 18:37:01 2017 +0300

    0029344: Foundation Classes, TCollection_AsciiString - replace confusing strncpy with memcpy
    
    strncpy() usage within TCollection_AsciiString has been replaced by memcpy()
    where string length has been already determined.
    
    TCollection_AsciiString::SetValue() now throws exception
    on attempt to set '\0' - TCollection_AsciiString::Trunc() should be used instead.
    TCollection_AsciiString(const char* , int ) has been modified to properly set string length
    in case of NULL-terminator appeared earlier then specified length.
    
    Interface_LineBuffer has been revised for using NCollection_Array1 instead of TCollection_AsciiString
    to avoid misusing TCollection_AsciiString interface.
04Author
05Commenter 2
Branch [archived branch] has been updated forcibly by Author.

[revision removed]
06Commenter 6
Reviewed, please integrate
07Commenter 7
Actually I have a doubt whether having null symbol in the middle of the string should really be prohibited...
08Author
> Actually I have a doubt whether having null symbol in the middle
> of the string should really be prohibited..
This is misconception - TCollection_AsciiString is supposed to know the length of the string to behave in deterministic way.
Although one would say that constant string class should not have method SetValue() at all.
Considering an allocation of buffer of greater size then actual string length, another mechanism might be considered - e.g. std::string has two properties: actual length and capacity.

09Commenter 1
Combination -
OCCT branch : [archived branch] SHA - [revision removed]
Products branch : [archived branch] SHA - [revision removed]
was compiled on Linux, MacOS and Windows platforms and tested on optimize mode.

Number of compiler warnings:
No new/fixed warnings

Regressions/Differences/Improvements:
No regressions/differences

CPU differences:
Debian70-64:
OCCT
Total CPU difference: 20052.220000000558 / 19837.270000000357 [+1.08%]
Products
Total CPU difference: 7995.2600000000875 / 8039.350000000084 [-0.55%]
Windows-64-VC10:
OCCT
Total CPU difference: 17873.174970898603 / 17832.614710898575 [+0.23%]
Products
Total CPU difference: 8036.500715699982 / 8019.621407499985 [+0.21%]


Image differences :
No differences that require special attention

Memory differences :
No differences that require special attention
10Commenter 2
Branch [archived branch] has been deleted by Author.

[revision removed]
11Commenter 2
Branch [archived branch] has been deleted by Author.

[revision removed]

Related records