View Issue Details

IDProjectCategoryView StatusLast Update
0009338mantisbtcsvpublic2010-12-17 04:40
Reportersveyret Assigned Todhx  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version1.1.1 
Target Version1.2.4Fixed in Version1.2.4 
Summary0009338: CSV export does not escape all characters
Description

The function csv_escape_string in csv_api.php only escapes strings which contain a column separator. But it should also escape the " character and the line termination characters.
Here is the modified function:

# --------------------
# escape a string before writing it to csv file.
function csv_escape_string( $p_str ) {
    $t_escaped = str_split( '"' . csv_get_separator() . csv_get_newline() );
    $t_must_escape = false;
    while( ( $t_char = current( $t_escaped ) ) !== false && !$t_must_escape ) {
        $t_must_escape = strpos( $p_str, $t_char ) !== false;
        next( $t_escaped ); 
    }
    if ( $t_must_escape ) {
        $p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
    }

    return $p_str;
}

I also join a diff file made against csv_api.php of Mantis version 1.1.1

Steps To Reproduce

Make a CSV export of values containing ", or CR LF. The result will not be a valid CSV file.

Tagspatch
Attached Files
csv_api.patch (378 bytes)   
56c56,62
< 		if ( strpos( $p_str, csv_get_separator() ) !== false ) {
---
> 		$t_escaped = str_split( '"' . csv_get_separator() . csv_get_newline() );
> 		$t_must_escape = false;
> 		while( ( $t_char = current( $t_escaped ) ) !== false && !$t_must_escape ) {
> 			$t_must_escape = strpos( $p_str, $t_char ) !== false;
> 			next( $t_escaped ); 
> 		}
> 		if ( $t_must_escape ) {
csv_api.patch (378 bytes)   
issue9338-1.1.x.patch (1,069 bytes)   
From 1f5c4f62c850b6edd2a72b54e21a923d372bb336 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?St=82phane=20Veyret?= <sveyret@axway.com>
Date: Wed, 1 Jul 2009 14:36:05 +0200
Subject: [PATCH] Fix 0009338: CSV export does not escape all characters

Adding escaping of newline in the CVS export
---
 core/csv_api.php |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/core/csv_api.php b/core/csv_api.php
index fa429b0..1777b01 100644
--- a/core/csv_api.php
+++ b/core/csv_api.php
@@ -53,7 +53,13 @@
 	# --------------------
 	# escape a string before writing it to csv file.
 	function csv_escape_string( $p_str ) {
-		if ( strpos( $p_str, csv_get_separator() ) !== false ) {
+		$t_escaped = str_split( '"' . csv_get_separator() . csv_get_newline() );
+		$t_must_escape = false;
+		while( ( $t_char = current( $t_escaped ) ) !== false && !$t_must_escape ) {
+			$t_must_escape = strpos( $p_str, $t_char ) !== false;
+			next( $t_escaped );
+		}
+		if ( $t_must_escape ) {
 			$p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
 		}
 
-- 
1.6.3.2

issue9338-1.1.x.patch (1,069 bytes)   
issue9338-master.patch (1,408 bytes)   
From a575fe210f0c4d53478f21cd078f00d97943a890 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?St=82phane=20Veyret?= <sveyret@axway.com>
Date: Wed, 1 Jul 2009 14:43:24 +0200
Subject: [PATCH] Fix 0009338: CSV export does not escape all characters

Changing escaping method (strings could be escaped twice)
---
 core/csv_api.php |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/core/csv_api.php b/core/csv_api.php
index 76885ed..d22ecab 100644
--- a/core/csv_api.php
+++ b/core/csv_api.php
@@ -72,18 +72,17 @@ function csv_get_default_filename() {
  * @access public
  */
 function csv_escape_string( $p_str ) {
-
-	# enclose strings with separators with quotaiton marks
-	if( strpos( $p_str, csv_get_separator() ) !== false ) {
-		$p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
-	}
-
-	# enclose multi-line strings with quotaiton marks
-	if( strpos( $p_str, "\n" ) !== false ) {
-		$p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
-	}
-
-	return $p_str;
+		$t_escaped = str_split( '"' . csv_get_separator() . csv_get_newline() );
+		$t_must_escape = false;
+		while( ( $t_char = current( $t_escaped ) ) !== false && !$t_must_escape ) {
+			$t_must_escape = strpos( $p_str, $t_char ) !== false;
+			next( $t_escaped );
+		}
+		if ( $t_must_escape ) {
+			$p_str = '"' . str_replace( '"', '""', $p_str ) . '"';
+		}
+
+		return $p_str;
 }
 
 /**
-- 
1.6.3.2

issue9338-master.patch (1,408 bytes)   

Activities

sveyret

sveyret

2009-07-01 08:52

reporter   ~0022345

I saw that a correction was done in the master branch, but this correction is escaping twice if there are both separator and newline in the text.

sveyret

sveyret

2009-07-01 10:10

reporter   ~0022347

I added two patches for branch 1.1.x and master…

kofman

kofman

2009-09-09 02:56

reporter   ~0022897

is there any progress on this issue? we are trying to integrate with Mantis, but this issues makes it impossible to parse CSV file correctly.

dhx

dhx

2010-09-18 00:46

reporter   ~0026764

Thanks Stéphane for the patch, I've finally committed it.

Sorry we didn't get around to committing it earlier, it must have become lost in the flow of bug reports.

Related Changesets

MantisBT: master 0f120c95

2009-07-01 08:43

sveyret

Committer: dhx


Details Diff
Fix 0009338: CSV export does not escape all characters

Changed escaping method (strings could be escaped twice).

Signed-off-by: David Hicks <hickseydr@optusnet.com.au>
Affected Issues
0009338
mod - core/csv_api.php Diff File

MantisBT: master-1.2.x 38c5a1c4

2009-07-01 08:43

sveyret

Committer: dhx


Details Diff
Fix 0009338: CSV export does not escape all characters

Changed escaping method (strings could be escaped twice).

Signed-off-by: David Hicks <hickseydr@optusnet.com.au>
Affected Issues
0009338
mod - core/csv_api.php Diff File