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

 
 

bug #33102: autosprintf needs an operator=

Submitted by:  None
Submitted on:  Sat 16 Apr 2011 05:41:21 PM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: Fixed
Privacy: PublicAssigned to: Miguel Ángel Arruga Vivas <m1gu3l>
Open/Closed: Closed

Add a New Comment (Rich MarkupRich Markup):
   

You are not logged in

Please log in, so followups can be emailed to you.

 

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

Miguel's patch pushed.

Daiki Ueno <ueno>
Project 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>
Project MemberIn charge of this item.
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>
Project MemberIn charge of this item.
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.)

Attach File(s):
   
   
Comment:
   

Attached Files
file #23262:  libasprintf-add-assignment-operator-to-autosprintf.patch added by None (2kB - 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
  • -unavailable- added by ueno (Posted a comment)
  • -unavailable- added by m1gu3l (Posted a comment)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 6 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Tue 05 Mar 2013 11:39:22 PM UTCuenoStatusReady For Test=>Fixed
      Open/ClosedOpen=>Closed
    Fri 01 Mar 2013 08:10:27 PM UTCm1gu3lStatusIn Progress=>Ready For Test
    Thu 28 Feb 2013 10:00:57 AM UTCm1gu3lStatusNone=>In Progress
      Assigned toNone=>m1gu3l
    Sat 16 Apr 2011 05:41:21 PM UTCNoneAttached File-=>Added libasprintf-add-assignment-operator-to-autosprintf.patch, #23262

    Back to the top


    Powered by Savane 3.1-cleanup