openimsdk/open-im-server · error

size must be greater than 0

Error message

size must be greater than 0

What it means

seqConversationMongo.Malloc allocates a block of sequence numbers for a conversation. It rejects negative sizes up front with this error, treating them as a caller bug; a size of 0 is allowed and simply returns the current max seq without allocating. This is an argument-validation guard, not a storage failure.

Source

Thrown at pkg/common/storage/database/mgo/seq_conversation.go:53

	insert := bson.M{
		"conversation_id": conversationID,
		"min_seq":         0,
		"max_seq":         0,
	}
	delete(insert, field)
	update := map[string]any{
		"$set": bson.M{
			field: seq,
		},
		"$setOnInsert": insert,
	}
	opt := options.Update().SetUpsert(true)
	return mongoutil.UpdateOne(ctx, s.coll, filter, update, false, opt)
}

func (s *seqConversationMongo) Malloc(ctx context.Context, conversationID string, size int64) (int64, error) {
	if size < 0 {
		return 0, errors.New("size must be greater than 0")
	}
	if size == 0 {
		return s.GetMaxSeq(ctx, conversationID)
	}
	filter := map[string]any{"conversation_id": conversationID}
	update := map[string]any{
		"$inc":         map[string]any{"max_seq": size},
		"$setOnInsert": map[string]any{"min_seq": int64(0)},
	}
	opt := options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After).SetProjection(map[string]any{"_id": 0, "max_seq": 1})
	lastSeq, err := mongoutil.FindOneAndUpdate[int64](ctx, s.coll, filter, update, opt)
	if err != nil {
		return 0, err
	}
	return lastSeq - size, nil
}

func (s *seqConversationMongo) SetMaxSeq(ctx context.Context, conversationID string, seq int64) error {

View on GitHub (pinned to 175a7bb067)

Solutions

  1. Inspect the caller's size computation and clamp negative values to 0 or skip the Malloc call entirely.
  2. Validate size > 0 (or >= 0) at the caller boundary before invoking Malloc.
  3. Check for integer underflow when size is derived from subtracting sequence numbers.

Example fix

// before
seq, err := seqConv.Malloc(ctx, convID, target-current)
// after
n := target - current
if n <= 0 {
    return nil
}
seq, err := seqConv.Malloc(ctx, convID, n)
Defensive patterns

Strategy: validation

Validate before calling

if size <= 0 {
    return errors.New("malloc size must be positive")
}

Type guard

func validMallocSize(size int64) bool { return size > 0 }

Prevention

When it happens

Trigger: Calling Malloc(ctx, conversationID, size) with size < 0, typically from computing an allocation count via subtraction (e.g. count = target - current) that underflows, or passing an uninitialized/negative int64.

Common situations: Message counters or batch allocators computed as a difference of seqs going negative; int64 underflow after wraparound; passing a config value that defaulted to -1 meaning 'unset'.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of openimsdk/open-im-server@175a7bb067 (2026-09-04). Data as JSON: /api/errors/d6b94ccf4cf04f3e. Report an issue: GitHub.