Skip to content

Commit

Permalink
#32 Abnormal execution result if the script has a null character(0x0)
Browse files Browse the repository at this point in the history
Fixed this bug to be trimmed null characters while saving a script.
But, I didn't consider to handle this when compile a script, so this
fixed didn't be a solution if there has already included a null
character in a script.
  • Loading branch information
newpcraft committed Mar 1, 2014
1 parent 5d1bb37 commit ff49b09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/
package org.ngrinder.common.util;

import java.io.IOException;
import java.nio.charset.Charset;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;

import java.io.IOException;
import java.nio.charset.Charset;

/**
* Automatic encoding detection utility.
*
Expand Down Expand Up @@ -63,4 +63,14 @@ public static String detectEncoding(byte[] data, String defaultEncoding) throws
boolean isReliable = Charset.isSupported(estimatedEncoding) && cm.getConfidence() >= MINIMAL_CONFIDENCE_LEVEL;
return isReliable ? estimatedEncoding : defaultEncoding;
}

/**
* Trim null characters in the string.
*
* @param string string
* @return
*/
public static String trimNullCharacter(String string) {
return string == null ? null : string.replaceAll("\0", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
package org.ngrinder.script.controller;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -52,7 +49,10 @@
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Lists.newArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.ngrinder.script.handler.ProjectHandler;
import org.ngrinder.script.handler.ScriptHandler;
import org.ngrinder.script.handler.ScriptHandlerFactory;
import org.ngrinder.script.model.FileCategory;
import org.ngrinder.script.model.FileEntry;
import org.ngrinder.script.model.FileType;
import org.ngrinder.script.repository.FileEntryRepository;
Expand Down Expand Up @@ -54,6 +55,7 @@
import static java.util.Collections.unmodifiableList;
import static org.ngrinder.common.util.CollectionUtils.buildMap;
import static org.ngrinder.common.util.CollectionUtils.newHashMap;
import static org.ngrinder.common.util.EncodingUtils.trimNullCharacter;
import static org.ngrinder.common.util.ExceptionUtils.processException;
import static org.ngrinder.common.util.Preconditions.checkNotEmpty;
import static org.ngrinder.common.util.Preconditions.checkNotNull;
Expand Down Expand Up @@ -240,12 +242,15 @@ public void addFolder(User user, String path, String folderName, String comment)
* Save File entry.
*
* @param user the user
* @param fileEntity fileEntity to be saved
* @param fileEntry fileEntry to be saved
*/
public void save(User user, FileEntry fileEntity) {
public void save(User user, FileEntry fileEntry) {
prepare(user);
checkNotEmpty(fileEntity.getPath());
fileEntityRepository.save(user, fileEntity, fileEntity.getEncoding());
checkNotEmpty(fileEntry.getPath());
if (fileEntry.getFileType().getFileCategory() == FileCategory.SCRIPT) {
fileEntry.setContent(trimNullCharacter(fileEntry.getContent()));
}
fileEntityRepository.save(user, fileEntry, fileEntry.getEncoding());
}

/**
Expand Down

0 comments on commit ff49b09

Please sign in to comment.