From 31a4a592a1a1170dadd2f94f8cf35cebe7a37cc1 Mon Sep 17 00:00:00 2001
From: Jung-uk Kim <jkim@FreeBSD.org>
Date: Thu, 28 Mar 2013 00:18:41 +0000
Subject: - Fix a buffer overflow in highlighter. [1] - Apply upstream patches
 to plug a memory leak, etc. - Move CLuceneConfig.cmake to DATADIR as other
 ports do. - Fix several problems found by Clang and make it build cleanly.

Submitted by:	Kishore Ramareddy (kishore at niksun dot com)
		(initial version) [1]
---
 textproc/clucene/Makefile                          |  5 ++
 textproc/clucene/files/patch-CMakeLists.txt        | 37 +++++++++++++-
 ...s-lib__CLucene__analysis__de__GermanStemmer.cpp | 20 +++++++-
 ...ribs-lib__CLucene__highlighter__Highlighter.cpp | 56 ++++++++++++++++++++++
 ...ibs-lib__CLucene__highlighter__WeightedTerm.cpp | 11 +++++
 ...atch-src__core__CLucene__index__IndexWriter.cpp | 11 +++++
 ...__core__CLucene__search__ConstantScoreQuery.cpp | 21 ++++++++
 ...patch-src__core__CLucene__util__MD5Digester.cpp | 11 +++++
 .../clucene/files/patch-src__core__CMakeLists.txt  |  4 +-
 .../files/patch-src__shared__CMakeLists.txt        | 11 ++++-
 ...c__shared__cmake__MacroCheckGccVisibility.cmake | 30 ++++++++++++
 textproc/clucene/pkg-plist                         |  4 +-
 12 files changed, 214 insertions(+), 7 deletions(-)
 create mode 100644 textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__Highlighter.cpp
 create mode 100644 textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__WeightedTerm.cpp
 create mode 100644 textproc/clucene/files/patch-src__core__CLucene__index__IndexWriter.cpp
 create mode 100644 textproc/clucene/files/patch-src__core__CLucene__search__ConstantScoreQuery.cpp
 create mode 100644 textproc/clucene/files/patch-src__core__CLucene__util__MD5Digester.cpp
 create mode 100644 textproc/clucene/files/patch-src__shared__cmake__MacroCheckGccVisibility.cmake

diff --git a/textproc/clucene/Makefile b/textproc/clucene/Makefile
index 7508a57c2058..b04abd3b79a0 100644
--- a/textproc/clucene/Makefile
+++ b/textproc/clucene/Makefile
@@ -3,6 +3,7 @@
 
 PORTNAME=	clucene
 PORTVERSION=	2.3.3.4
+PORTREVISION=	1
 CATEGORIES=	textproc
 MASTER_SITES=	SF/${PORTNAME}/${PORTNAME}-core-unstable/2.3
 DISTNAME=	${PORTNAME}-core-${PORTVERSION}
@@ -22,4 +23,8 @@ USE_LDCONFIG=	yes
 CMAKE_ARGS=	-DBUILD_CONTRIBS_LIB=ON
 CXXFLAGS+=	-D__LONG_LONG_SUPPORTED
 
+post-patch:
+	@${REINPLACE_CMD} -e 's|%%DATADIR%%|${DATADIR_REL}|' \
+	    ${WRKSRC}/src/core/CMakeLists.txt
+
 .include <bsd.port.mk>
diff --git a/textproc/clucene/files/patch-CMakeLists.txt b/textproc/clucene/files/patch-CMakeLists.txt
index 397c374d661a..4fb8629bd0b5 100644
--- a/textproc/clucene/files/patch-CMakeLists.txt
+++ b/textproc/clucene/files/patch-CMakeLists.txt
@@ -1,5 +1,40 @@
 --- CMakeLists.txt.orig	2011-03-16 20:21:07.000000000 -0400
-+++ CMakeLists.txt	2012-08-07 16:27:55.000000000 -0400
++++ CMakeLists.txt	2013-03-27 15:33:37.000000000 -0400
+@@ -62,14 +62,14 @@
+   OFF)
+   
+ SET(ENABLE_ANSI_MODE OFF)
+-IF(CMAKE_COMPILER_IS_GNUCXX)
++IF(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
+   SET(ENABLE_ANSI_MODE ON)
+   
+   #exceptions:
+   IF(MINGW OR CYGWIN)
+     SET(ENABLE_ANSI_MODE OFF)
+   ENDIF(MINGW OR CYGWIN)
+-ENDIF(CMAKE_COMPILER_IS_GNUCXX)
++ENDIF(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
+ 
+ OPTION(ENABLE_ANSI_MODE
+   "compile with -ansi flag"
+@@ -109,7 +109,7 @@
+ 
+ #check flags...
+ INCLUDE (TestCXXAcceptsFlag)
+-IF ( CMAKE_COMPILER_IS_GNUCC )
++IF ( CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang" )
+     CHECK_CXX_ACCEPTS_FLAG(-pg GccFlagPg)
+     IF ( GccFlagPg )
+         OPTION(ENABLE_GPROF
+@@ -131,7 +131,7 @@
+    IF( ENABLE_ANSI_MODE )
+     SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ansi")
+    ENDIF ( ENABLE_ANSI_MODE )
+-ENDIF(CMAKE_COMPILER_IS_GNUCC) 
++ENDIF(CMAKE_COMPILER_IS_GNUCC OR "${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
+ 
+ 
+ #Single output directory for building all executables and libraries.
 @@ -163,7 +163,7 @@
    SET(BUILD_CONTRIBS_LIB 1)
  ENDIF ( BUILD_CONTRIBS )
diff --git a/textproc/clucene/files/patch-src__contribs-lib__CLucene__analysis__de__GermanStemmer.cpp b/textproc/clucene/files/patch-src__contribs-lib__CLucene__analysis__de__GermanStemmer.cpp
index 96e809856cdd..27b80ce58ce6 100644
--- a/textproc/clucene/files/patch-src__contribs-lib__CLucene__analysis__de__GermanStemmer.cpp
+++ b/textproc/clucene/files/patch-src__contribs-lib__CLucene__analysis__de__GermanStemmer.cpp
@@ -1,5 +1,23 @@
 --- src/contribs-lib/CLucene/analysis/de/GermanStemmer.cpp.orig	2011-03-16 20:21:07.000000000 -0400
-+++ src/contribs-lib/CLucene/analysis/de/GermanStemmer.cpp	2013-01-08 17:03:27.000000000 -0500
++++ src/contribs-lib/CLucene/analysis/de/GermanStemmer.cpp	2013-03-27 17:12:31.000000000 -0400
+@@ -16,7 +16,7 @@
+     }
+ 
+     TCHAR* GermanStemmer::stem(const TCHAR* term, size_t length) {
+-      if (length < 0) {
++      if (length <= 0) {
+         length = _tcslen(term);
+       }
+ 
+@@ -38,7 +38,7 @@
+     }
+ 
+     bool GermanStemmer::isStemmable(const TCHAR* term, size_t length) const {
+-      if (length < 0) {
++      if (length <= 0) {
+         length = _tcslen(term);
+       }
+       for (size_t c = 0; c < length; c++) {
 @@ -144,7 +144,7 @@
            {
              buffer.setCharAt( i, _T('$') );
diff --git a/textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__Highlighter.cpp b/textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__Highlighter.cpp
new file mode 100644
index 000000000000..5f15a2305659
--- /dev/null
+++ b/textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__Highlighter.cpp
@@ -0,0 +1,56 @@
+--- src/contribs-lib/CLucene/highlighter/Highlighter.cpp.orig	2011-03-16 20:21:07.000000000 -0400
++++ src/contribs-lib/CLucene/highlighter/Highlighter.cpp	2013-03-27 19:05:00.000000000 -0400
+@@ -214,8 +214,9 @@
+ 			int32_t startOffset;
+ 			int32_t endOffset;
+ 			int32_t lastEndOffset = 0;
++			int len;
+ 			_textFragmenter->start(text);
+-			TCHAR substringBuffer[LUCENE_MAX_WORD_LEN];
++			TCHAR substringBuffer[LUCENE_MAX_WORD_LEN+1];
+ 
+ 			TokenGroup* tokenGroup=_CLNEW TokenGroup();
+ 
+@@ -229,8 +230,9 @@
+ 					 startOffset = tokenGroup->getStartOffset();
+ 					 endOffset = tokenGroup->getEndOffset();
+ 
+-					 _tcsncpy(substringBuffer,text+startOffset,endOffset-startOffset);
+-					 substringBuffer[endOffset-startOffset]=_T('\0');
++					 len = cl_min(endOffset-startOffset,LUCENE_MAX_WORD_LEN);
++					 _tcsncpy(substringBuffer,text+startOffset,len);
++					 substringBuffer[len]=_T('\0');
+ 
+ 					 TCHAR* encoded = _encoder->encodeText(substringBuffer);
+ 					 TCHAR* markedUpText=_formatter->highlightTerm(encoded, tokenGroup);
+@@ -238,9 +240,7 @@
+ 
+ 					 //store any whitespace etc from between this and last group
+ 					 if (startOffset > lastEndOffset){
+-						 int len = startOffset-lastEndOffset;
+-						 if ( len > LUCENE_MAX_FIELD_LEN )
+-							 len = LUCENE_MAX_FIELD_LEN;
++						 len = cl_min(startOffset-lastEndOffset,LUCENE_MAX_FIELD_LEN);
+ 						 _tcsncpy(buffer,text+lastEndOffset,len);
+ 						 buffer[len]=_T('\0');
+ 
+@@ -287,8 +287,9 @@
+   	            startOffset = tokenGroup->getStartOffset();
+   	            endOffset = tokenGroup->getEndOffset();
+ 
+-				_tcsncpy(substringBuffer,text+startOffset,endOffset-startOffset);
+-				substringBuffer[endOffset-startOffset]=_T('\0');
++		    len = cl_min(endOffset-startOffset,LUCENE_MAX_WORD_LEN);
++		    _tcsncpy(substringBuffer,text+startOffset,len);
++		    substringBuffer[len]=_T('\0');
+ 
+ 				TCHAR* encoded = _encoder->encodeText(substringBuffer);
+         TCHAR* markedUpText=_formatter->highlightTerm(encoded, tokenGroup);
+@@ -393,6 +394,7 @@
+ 				}
+ 			}
+ 		)
++		return NULL;
+ 	}
+ 
+ 
diff --git a/textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__WeightedTerm.cpp b/textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__WeightedTerm.cpp
new file mode 100644
index 000000000000..f0bc5aa704a2
--- /dev/null
+++ b/textproc/clucene/files/patch-src__contribs-lib__CLucene__highlighter__WeightedTerm.cpp
@@ -0,0 +1,11 @@
+--- src/contribs-lib/CLucene/highlighter/WeightedTerm.cpp.orig	2011-03-16 20:21:07.000000000 -0400
++++ src/contribs-lib/CLucene/highlighter/WeightedTerm.cpp	2013-03-27 16:51:33.000000000 -0400
+@@ -73,7 +73,7 @@
+ 	 * @param weight the weight associated with this term
+ 	 */
+ 	void WeightedTerm::setWeight(float_t weight) {
+-		this->_weight = _weight;
++		_weight = weight;
+ 		cachedHashCode = 0;
+ 	}
+ 
diff --git a/textproc/clucene/files/patch-src__core__CLucene__index__IndexWriter.cpp b/textproc/clucene/files/patch-src__core__CLucene__index__IndexWriter.cpp
new file mode 100644
index 000000000000..cb8aab7e7dd7
--- /dev/null
+++ b/textproc/clucene/files/patch-src__core__CLucene__index__IndexWriter.cpp
@@ -0,0 +1,11 @@
+--- src/core/CLucene/index/IndexWriter.cpp.orig	2011-03-16 20:21:07.000000000 -0400
++++ src/core/CLucene/index/IndexWriter.cpp	2013-03-27 16:56:54.000000000 -0400
+@@ -814,7 +814,7 @@
+   ensureOpen();
+ 
+   if (maxNumSegments < 1)
+-    _CLTHROWA(CL_ERR_IllegalArgument, "maxNumSegments must be >= 1; got " + maxNumSegments);
++    _CLTHROWA(CL_ERR_IllegalArgument, (string("maxNumSegments must be >= 1; got ") + Misc::toString(maxNumSegments)).c_str());
+ 
+   if (infoStream != NULL)
+     message("optimize: index now " + segString());
diff --git a/textproc/clucene/files/patch-src__core__CLucene__search__ConstantScoreQuery.cpp b/textproc/clucene/files/patch-src__core__CLucene__search__ConstantScoreQuery.cpp
new file mode 100644
index 000000000000..a11428b60420
--- /dev/null
+++ b/textproc/clucene/files/patch-src__core__CLucene__search__ConstantScoreQuery.cpp
@@ -0,0 +1,21 @@
+--- src/core/CLucene/search/ConstantScoreQuery.cpp.orig	2011-03-16 20:21:07.000000000 -0400
++++ src/core/CLucene/search/ConstantScoreQuery.cpp	2013-03-27 17:34:06.000000000 -0400
+@@ -25,14 +25,17 @@
+     BitSet* bits;
+     const float_t theScore;
+     int32_t _doc;
++    bool shouldDelete;
+ 
+ public:
+     ConstantScorer(Similarity* similarity, IndexReader* reader, Weight* w, Filter* filter) : Scorer(similarity),
+         bits(filter->bits(reader)), theScore(w->getValue()), _doc(-1)
+     {
++        shouldDelete = filter->shouldDeleteBitSet(bits);
+     }
+     virtual ~ConstantScorer() {
+-        _CLLDELETE(bits);
++        if ( shouldDelete)
++            _CLLDELETE(bits);
+     }
+ 
+     bool next() {
diff --git a/textproc/clucene/files/patch-src__core__CLucene__util__MD5Digester.cpp b/textproc/clucene/files/patch-src__core__CLucene__util__MD5Digester.cpp
new file mode 100644
index 000000000000..aeba9690f115
--- /dev/null
+++ b/textproc/clucene/files/patch-src__core__CLucene__util__MD5Digester.cpp
@@ -0,0 +1,11 @@
+--- src/core/CLucene/util/MD5Digester.cpp.orig	2011-03-16 20:21:07.000000000 -0400
++++ src/core/CLucene/util/MD5Digester.cpp	2013-03-27 17:16:18.000000000 -0400
+@@ -82,7 +82,7 @@
+ 	for (nCount = 0; nCount < 16; nCount++)
+ 	{
+ 		cl_sprintf(chEach, 10, "%02x", md5Digest[nCount]);
+-		strncat(chBuffer, chEach, sizeof(chEach));
++		strncat(chBuffer, chEach, 2);
+ 	}
+ 
+ 	return STRDUP_AtoA(chBuffer);
diff --git a/textproc/clucene/files/patch-src__core__CMakeLists.txt b/textproc/clucene/files/patch-src__core__CMakeLists.txt
index b38690d1ad38..9aa47e90ad18 100644
--- a/textproc/clucene/files/patch-src__core__CMakeLists.txt
+++ b/textproc/clucene/files/patch-src__core__CMakeLists.txt
@@ -1,11 +1,11 @@
 --- src/core/CMakeLists.txt.orig	2011-03-16 20:21:07.000000000 -0400
-+++ src/core/CMakeLists.txt	2012-08-07 14:12:31.000000000 -0400
++++ src/core/CMakeLists.txt	2013-03-27 16:32:49.000000000 -0400
 @@ -252,13 +252,13 @@
      set(CLUCENE_SOVERSION ${CLUCENE_SOVERSION})
  ")
  install(FILES "${CMAKE_CURRENT_BINARY_DIR}/CLuceneConfig.cmake"
 -        DESTINATION ${LIB_DESTINATION}/CLuceneConfig.cmake)
-+        DESTINATION ${LIB_DESTINATION}/CLucene)
++        DESTINATION %%DATADIR%%)
  
  # install pkg-config file
  IF(NOT WIN32)
diff --git a/textproc/clucene/files/patch-src__shared__CMakeLists.txt b/textproc/clucene/files/patch-src__shared__CMakeLists.txt
index 7bd76230e642..51810cfb170e 100644
--- a/textproc/clucene/files/patch-src__shared__CMakeLists.txt
+++ b/textproc/clucene/files/patch-src__shared__CMakeLists.txt
@@ -1,5 +1,14 @@
 --- src/shared/CMakeLists.txt.orig	2011-03-16 20:21:07.000000000 -0400
-+++ src/shared/CMakeLists.txt	2012-08-07 13:31:00.000000000 -0400
++++ src/shared/CMakeLists.txt	2013-03-27 17:31:13.000000000 -0400
+@@ -42,7 +42,7 @@
+ find_package(ZLIB)
+ IF ( ZLIB_FOUND )
+   SET ( EXTRA_LIBS ${EXTRA_LIBS} ${ZLIB_LIBRARY} )
+-ELSEIF ( ZLIB_FOUND )
++ELSE ( ZLIB_FOUND )
+   MESSAGE( "ZLIB not found, using local: ${clucene-ext_SOURCE_DIR}/zlib" )
+   SET(ZLIB_INCLUDE_DIR ${clucene-ext_SOURCE_DIR}/zlib )
+   SET(ZLIB_LIBRARY ${clucene-ext_BINARY_DIR})
 @@ -62,7 +62,7 @@
                          stdint.h unistd.h io.h direct.h sys/dir.h sys/ndir.h dirent.h wctype.h fcntl.h
                          stat.h sys/stat.h stdexcept errno.h fcntl.h windef.h windows.h wchar.h 
diff --git a/textproc/clucene/files/patch-src__shared__cmake__MacroCheckGccVisibility.cmake b/textproc/clucene/files/patch-src__shared__cmake__MacroCheckGccVisibility.cmake
new file mode 100644
index 000000000000..d9d9b0b1c5eb
--- /dev/null
+++ b/textproc/clucene/files/patch-src__shared__cmake__MacroCheckGccVisibility.cmake
@@ -0,0 +1,30 @@
+--- src/shared/cmake/MacroCheckGccVisibility.cmake.orig	2011-03-16 20:21:07.000000000 -0400
++++ src/shared/cmake/MacroCheckGccVisibility.cmake	2013-03-27 15:02:55.000000000 -0400
+@@ -6,7 +6,7 @@
+ # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+ 
+ macro(MACRO_CHECK_GCC_VISIBILITY GccVisibility)
+-  if (CMAKE_COMPILER_IS_GNUCXX)
++  if (CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
+    include(CheckCXXCompilerFlag)
+    include(MacroEnsureVersion)
+    # visibility support
+@@ -43,7 +43,7 @@
+ 
+    if (${GccVisibility} AND GCC_IS_NEWER_THAN_4_1 AND NOT _GCC_COMPILED_WITH_BAD_ALLOCATOR)
+       set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
+-      set (KDE4_C_FLAGS "${KDE4_C_FLAGS}" "-fvisibility=hidden")
++      set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" "-fvisibility=hidden")
+ 
+       if (GCC_IS_NEWER_THAN_4_2)
+           set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
+@@ -52,7 +52,7 @@
+       set (${GccVisibility} 0)
+    endif (${GccVisibility} AND GCC_IS_NEWER_THAN_4_1 AND NOT _GCC_COMPILED_WITH_BAD_ALLOCATOR)
+ 
+-  else (CMAKE_COMPILER_IS_GNUCXX)
++  else (CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
+     set(${GccVisibility} FALSE)
+-  endif (CMAKE_COMPILER_IS_GNUCXX)
++  endif (CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
+ endmacro(MACRO_CHECK_GCC_VISIBILITY)
diff --git a/textproc/clucene/pkg-plist b/textproc/clucene/pkg-plist
index 6424dbacdb6b..a56453858163 100644
--- a/textproc/clucene/pkg-plist
+++ b/textproc/clucene/pkg-plist
@@ -348,7 +348,6 @@ include/CLucene/util/byteinputstream.h
 include/CLucene/util/gzipcompressstream.h
 include/CLucene/util/gzipinputstream.h
 include/CLucene/util/streamarray.h
-lib/CLucene/CLuceneConfig.cmake
 lib/libclucene-contribs-lib.so
 lib/libclucene-contribs-lib.so.1
 lib/libclucene-contribs-lib.so.2.3.3.4
@@ -359,7 +358,8 @@ lib/libclucene-shared.so
 lib/libclucene-shared.so.1
 lib/libclucene-shared.so.2.3.3.4
 libdata/pkgconfig/libclucene-core.pc
-@dirrm lib/CLucene
+%%DATADIR%%/CLuceneConfig.cmake
+@dirrm %%DATADIR%%
 @dirrm include/CLucene/util
 @dirrm include/CLucene/store
 @dirrm include/CLucene/snowball/src_c
-- 
cgit debian/1.2.3+git2.25.1-1-2-gaceb0