bugGNU gettext - Bugs: bug #33102, autosprintf needs an operator=

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #33102: autosprintf needs an operator=

Submitter:  None
Submitted:  Sat 16 Apr 2011 05:41:21 PM UTC
   
 
Category:  Programmer interface Severity:  3 - Normal
Item Group:  None Status:  Fix Released
Privacy:  Public Assigned to:  m1gu3l
Open/Closed:  Closed

Tue 05 Mar 2013 11:39:22 PM UTC, comment #3: 

Miguel's patch pushed.

Daiki Ueno <ueno>
Group administrator
Fri 01 Mar 2013 08:10:27 PM UTC, comment #2: 

It can be tested at https://github.com/644rosen/gettext_gtkbuilder_support.git in the branch libasprintf and in master

Miguel Ángel Arruga Vivas <m1gu3l>
Group Member
Tue 29 Jan 2013 06:02:28 PM UTC, comment #1: 

This is a real bug.
You can get more info at http://www.cplusplus.com/articles/y8hv0pDG/ or http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

The default copy constructor is the same as struct copy in C, so the same pointer is "free"d twice in the example code. Also a pointer to the heap is lost, so there is a memory leak.

With C++11, the keyword delete can be used to forbid copy with '=', or also the operator can be declared private for C++98 compatibility. This will forbid autosprintf assignation:

Error: Compiler error at
x = y;
^^^^

The other way (it is really hard the final implementation differs too much from the patch) allows copy by '=', that also may be wanted at some scenarios.

x = y; // The pointer y->str is not copied to x->str
// Only the contents of y->str are copied to a newly allocated x->str

Miguel Ángel Arruga Vivas <m1gu3l>
Group Member
Sat 16 Apr 2011 05:41:21 PM UTC, original submission:  

The C++ class autosprintf has a non-trivial destructor and copy constructor, and so by the Rule of Three it also needs an assignment operator.  Without this, the following code fragment currently causes a double free when the second object destructs.

gnu::autosprintf x("hi");
gnu::autosprintf y("hello");
x = y;

Attached a straightforward implementation.  Valgrind is happy. Not entirely sure I got the ChangeLog format right though.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #23262:  libasprintf-add-assignment-operator-to-autosprintf.patch added by None (2KiB - text/x-patch - libasprintf: add assignment operator to autosprintf)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by haible (Updated the item)
  • -email is unavailable- added by ueno (Posted a comment)
  • -email is unavailable- added by m1gu3l (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-01-04 haible StatusFixed Fix Released
    2016-11-27 haible CategoryNone Programmer interface
    2013-03-05 ueno StatusReady For Test Fixed
        Open/ClosedOpen Closed
    2013-03-01 m1gu3l StatusIn Progress Ready For Test
    2013-02-28 m1gu3l StatusNone In Progress
        Assigned toNone m1gu3l
    2011-04-16 None Attached File- Added libasprintf-add-assignment-operator-to-autosprintf.patch, #23262

    Back to the top

    Powered by Savane 3.13-4b48.
    Corresponding source code