hyperledger/fabric · error

error compiling regexp: %s

Error message

error compiling regexp: %s

What it means

This wraps a regexp.Compile failure of the constant expectedDatabaseNamePattern used to validate CouchDB database names. Since the pattern is a compile-time constant in the library, this error should be impossible in normal operation; it only occurs if the pattern constant is edited to an invalid regex (e.g. during customization).

Source

Thrown at core/ledger/kvledger/txmgmt/statedb/statecouchdb/couchdbutil.go:288

// _, $, (, ), +, -, and / are allowed. Must begin with a letter.
//
// Restrictions have already been applied to the database name from Orderer based on
// restrictions required by Kafka and couchDB (except a '.' char). The databaseName
// passed in here is expected to follow `[a-z][a-z0-9.$_()+-]*` pattern.
//
// This validation will simply check whether the database name matches the above pattern and will replace
// all occurrence of '.' by '$'. This will not cause collisions in the transformed named
func mapAndValidateDatabaseName(databaseName string) (string, error) {
	// test Length
	if len(databaseName) <= 0 {
		return "", errors.Errorf("database name is illegal, cannot be empty")
	}
	if len(databaseName) > maxLength {
		return "", errors.Errorf("database name is illegal, cannot be longer than %d", maxLength)
	}
	re, err := regexp.Compile(expectedDatabaseNamePattern)
	if err != nil {
		return "", errors.Wrapf(err, "error compiling regexp: %s", expectedDatabaseNamePattern)
	}
	matched := re.FindString(databaseName)
	if len(matched) != len(databaseName) {
		return "", errors.Errorf("databaseName '%s' does not match pattern '%s'", databaseName, expectedDatabaseNamePattern)
	}
	// replace all '.' to '$'. The databaseName passed in will never contain an '$'.
	// So, this translation will not cause collisions
	databaseName = strings.Replace(databaseName, ".", "$", -1)
	return databaseName, nil
}

// escapeUpperCase replaces every upper case letter with a '$' and the respective
// lower-case letter
func escapeUpperCase(dbName string) string {
	re := regexp.MustCompile(`([A-Z])`)
	dbName = re.ReplaceAllString(dbName, "$$"+"$1")
	return strings.ToLower(dbName)
}

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Revert or correct the expectedDatabaseNamePattern constant in couchdbutil.go to a valid regular expression.
  2. Test the regex at regex101.com or in a Go scratch program before committing the change.
  3. Rebuild the peer from unmodified upstream source to eliminate the customization.

Example fix

// before (invalid pattern)
const expectedDatabaseNamePattern = "^[a-z][a-z0-9-$()_+]*[$(" 
// after
const expectedDatabaseNamePattern = "^[a-z][a-z0-9-$()_+]*$"
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity-check any custom pattern before building
if _, err := regexp.Compile(expectedDatabaseNamePattern); err != nil { panic("invalid db name pattern") }

Try / catch

if err != nil {
    if strings.Contains(err.Error(), "error compiling regexp") {
        // pattern constant was customized incorrectly; revert to upstream
    }
    return err
}

Prevention

When it happens

Trigger: regexp.Compile(expectedDatabaseNamePattern) returns an error — only possible when the source constant expectedDatabaseNamePattern has been modified to a syntactically invalid regular expression.

Common situations: Developers patching Fabric source to relax database-name rules and introducing a malformed regex; vendored/patched builds.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/37b112102bd3da9b. Report an issue: GitHub.