After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 156423 - Saving in UTF-16 encoding destroys files
Saving in UTF-16 encoding destroys files
Status: RESOLVED FIXED
Product: gedit
Classification: Applications
Component: general
2.8.x
Other Linux
: High critical
: ---
Assigned To: Gedit maintainers
gedit QA volunteers
: 156130 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2004-10-25 20:48 UTC by Loïc Minier
Modified: 2005-01-25 19:03 UTC
See Also:
GNOME target: ---
GNOME version: 2.7/2.8


Attachments
Patch against CVS HEAD (4.09 KB, patch)
2005-01-25 16:11 UTC, Paolo Maggi
none Details | Review
patch (3.98 KB, patch)
2005-01-25 17:57 UTC, Paolo Borelli
none Details | Review
Patch against CVS HEAD (v.2) (8.44 KB, patch)
2005-01-25 18:29 UTC, Paolo Maggi
none Details | Review
Committed patch (9.84 KB, patch)
2005-01-25 19:02 UTC, Paolo Maggi
none Details | Review

Description Loïc Minier 2004-10-25 20:48:44 UTC
1. Create a file of some length (a sentence for example) with gedit
2. Make sure the UTF-16 charset is available in the Save as dialog
3. Save as UTF-16
4. Check the saved file, it's 2 chars long

Reported against 2.6, also happens in 2.8.

This is Debian bug <http://bugs.debian.org/261622>.
  Regards,
-- 
Loïc Minier
Comment 1 Paolo Maggi 2004-10-25 21:05:58 UTC
I can reproduce this bug on Fedora Core 2 too.
Comment 2 Paolo Maggi 2004-10-25 21:24:58 UTC
This is due to the fact that UTF-16 encoded text may contain 0x00 bytes.
So the following code in gedit-document.c (gedit_document_save_as_real function)
is wrong:

	if (encoding != gedit_encoding_get_utf8 ())
	{
		GError *conv_error = NULL;
		gchar* converted_file_contents = NULL;

		converted_file_contents = gedit_convert_from_utf8 (chars, 
								   -1, 
								   encoding,
								   &conv_error);

		if (conv_error != NULL)
		{
			/* Conversion error */
			g_propagate_error (error, conv_error);

			close (fd);
			unlink (temp_filename);
			goto out;
		}
		else
		{
			g_free (chars);
			chars = converted_file_contents;
		}
	}

	chars_len = strlen (chars);

	add_cr = (*(chars + chars_len - 1) != '\n');

	/* Save the file content */
	res = (write (fd, chars, chars_len) == chars_len);

	if (res && add_cr)
		/* Add \n if needed */
		res = (write (fd, "\n", 1) == 1);



The only way I see to solve this problem is to modify gedit_convert_from_utf8 so
that it returns the length in bytes of the converted text.
Note that we must add the closing "\n" before converting the text to the new
encoding.
Comment 3 Paolo Maggi 2004-10-31 22:15:30 UTC
*** Bug 156130 has been marked as a duplicate of this bug. ***
Comment 4 Paolo Borelli 2004-12-06 08:51:46 UTC
is this a related (dup?) of http://bugzilla.gnome.org/show_bug.cgi?id=126555
Comment 5 Paolo Maggi 2004-12-06 09:39:32 UTC
no, it isn't.
Comment 6 Paolo Maggi 2005-01-25 16:11:56 UTC
Created attachment 36509 [details] [review]
Patch against CVS HEAD

The attached patch solves the problem.
It is still not ready to be committed since it breaks the savecopy plugin and
needs some polishment too.
Comment 7 Paolo Borelli 2005-01-25 17:57:46 UTC
Created attachment 36511 [details] [review]
patch

just comipile tested
Comment 8 Paolo Maggi 2005-01-25 18:29:58 UTC
Created attachment 36513 [details] [review]
Patch against CVS HEAD (v.2)

This patch merges the previous two patches and fixed a couple of problems.
Comment 9 Paolo Maggi 2005-01-25 19:02:20 UTC
Created attachment 36515 [details] [review]
Committed patch
Comment 10 Paolo Maggi 2005-01-25 19:03:43 UTC
Fixed in CVS HEAD.