Skip to content

Custom clean filter goes into infinite loop while processing large files #261

@ssamirah

Description

@ssamirah

Version

7.4.0.202509020913-r

Operating System

Windows

Bug description

My requirement is to create a custom filter where a particular file type extension is processed and the output is added to the repository.
I modified the git configuration to add custom filter and also appended the relevant extension to git attributes. My clean filter gets invoked however it goes into infinite loop.
I removed all logic for processing and just wrote the inputstream to the output, this also resulted in same behaviour. when I tried to read and write a small text file however it was successful. My simplified filter code which reads the inputstream and writes it to the output stream:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.jgit.attributes.FilterCommand;
import org.eclipse.jgit.attributes.FilterCommandFactory;
import org.eclipse.jgit.attributes.FilterCommandRegistry;
import org.eclipse.jgit.lib.Repository;

/**

  • The Class CustomJgitCleanFilter.

*/
public class CustomJgitCleanFilter extends FilterCommand {

/** The Constant FACTORY. */
public static final FilterCommandFactory FACTORY = CustomJgitCleanFilter::new;
/**
 * Register.
 */
static void register() {
	FilterCommandRegistry.register("jgit://filter/purge/"
			+ org.eclipse.jgit.lib.Constants.ATTR_FILTER_TYPE_CLEAN, FACTORY);
}

/**
 * Instantiates a new scg jgit pre process filter.
 * @param db the db
 * @param in the in
 * @param out the out
 * @throws IOException Signals that an I/O exception has occurred.
 */
public CustomJgitCleanFilter( Repository db,InputStream in, OutputStream out) throws IOException {
	super(in, out);
}

/**
 * Run.
 * @return the int
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Override
public int run() throws IOException {
	int bytesRead;
    int bigBufferSize = 8192; 
 byte[] bigBuffer = new byte[bigBufferSize];
		while ((bytesRead = this.in.read(bigBuffer)) != -1) {
			if(bytesRead>0) {
			this.out.write(bigBuffer, 0, bytesRead);
			}
		}
		return bytesRead;
	
}

}

Actual behavior

The issue seems to be from this piece of code in org.eclipse.jgit.treewalk.WorkingTreeIterator -
private static long computeLength(InputStream in) throws IOException {
// Since we only care about the length, use skip. The stream
// may be able to more efficiently wade through its data.
//
long length = 0;
for (;;) {
long n = in.skip(1 << 20);
if (n <= 0)
break;
length += n;
}
return length;
}
For smaller sized files this block is not invoked and gets processed, however for files larger than 65536 bytes this issue occurs, The issue is reproduceable with a simple zip file larger than 65536 bytes.**

Expected behavior

The process should end smoothly or in case of any exception should throw error instead of goin into an infinite loop.

Relevant log output

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions