Skip to content

Commit 06960e0

Browse files
committed
Make cleaner for callers by not exposing success parameter - callers can decide whether to take different action on nilPtr
Signed-off-by: Dave Crighton <dave.crighton@kaleido.io>
1 parent 0b1d192 commit 06960e0

3 files changed

Lines changed: 14 additions & 26 deletions

File tree

internal/operations/operation_updater.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ func (ou *operationUpdater) resolveOperation(ctx context.Context, ns string, id
422422
if status != "" {
423423
update = update.Set("status", status)
424424
}
425-
if safeErr, ok := utils.DBSafeUTF8StringFromPtr(ctx, errorMsg); ok {
426-
update = update.Set("error", safeErr)
425+
if errorMsg != nil {
426+
update = update.Set("error", utils.DBSafeUTF8StringFromPtr(ctx, errorMsg))
427427
}
428428
if output != nil {
429429
update = update.Set("output", output)

pkg/utils/dbstring.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ import (
2525
"github.com/hyperledger/firefly-common/pkg/log"
2626
)
2727

28-
// DBSafeUTF8StringFromPtr returns a DB-safe UTF-8 string from a pointer, and true if the pointer
29-
// was non-nil. Strings containing invalid UTF-8 sequences or null bytes (which PostgreSQL rejects
30-
// in text columns even though null bytes are technically valid UTF-8) are hex-encoded instead,
31-
// with a warning logged.
32-
func DBSafeUTF8StringFromPtr(ctx context.Context, s *string) (string, bool) {
28+
// DBSafeUTF8StringFromPtr returns a DB-safe UTF-8 string from a pointer.
29+
// Nil pointers return "". Strings containing invalid UTF-8 sequences or null
30+
// bytes (which PostgreSQL rejects in text columns even though null bytes are
31+
// technically valid UTF-8) are hex-encoded instead, with a warning logged.
32+
func DBSafeUTF8StringFromPtr(ctx context.Context, s *string) string {
3333
if s == nil {
34-
return "", false
34+
return ""
3535
}
3636
if !utf8.ValidString(*s) || strings.ContainsRune(*s, 0) {
3737
hexString := hex.EncodeToString([]byte(*s))
3838
log.L(ctx).Warnf("String contains invalid UTF-8 or null bytes - encoding as hex: %s", hexString)
39-
return hexString, true
39+
return hexString
4040
}
41-
return *s, true
41+
return *s
4242
}

pkg/utils/dbstring_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,20 @@ import (
2424
)
2525

2626
func TestDBSafeUTF8StringFromPtrNil(t *testing.T) {
27-
s, ok := DBSafeUTF8StringFromPtr(context.Background(), nil)
28-
assert.False(t, ok)
29-
assert.Equal(t, "", s)
27+
assert.Equal(t, "", DBSafeUTF8StringFromPtr(context.Background(), nil))
3028
}
3129

3230
func TestDBSafeUTF8StringFromPtrValid(t *testing.T) {
3331
msg := "FF23021: EVM reverted: some normal error message"
34-
s, ok := DBSafeUTF8StringFromPtr(context.Background(), &msg)
35-
assert.True(t, ok)
36-
assert.Equal(t, msg, s)
32+
assert.Equal(t, msg, DBSafeUTF8StringFromPtr(context.Background(), &msg))
3733
}
3834

3935
func TestDBSafeUTF8StringFromPtrInvalidUTF8(t *testing.T) {
40-
// Simulate the actual revert scenario: readable text with embedded ABI-encoded Error(string)
41-
// selector bytes (0x08, 0xc3, 0x79, 0xa0) and null byte padding, which is invalid UTF-8
4236
msg := "[OCPE]404/98 - \x08\xc3\x79\xa0\x00\x00\x00[TMM]404/16e"
43-
s, ok := DBSafeUTF8StringFromPtr(context.Background(), &msg)
44-
assert.True(t, ok)
45-
assert.Equal(t, "5b4f4350455d3430342f3938202d2008c379a00000005b544d4d5d3430342f313665", s)
37+
assert.Equal(t, "5b4f4350455d3430342f3938202d2008c379a00000005b544d4d5d3430342f313665", DBSafeUTF8StringFromPtr(context.Background(), &msg))
4638
}
4739

4840
func TestDBSafeUTF8StringFromPtrNullBytesInValidUTF8(t *testing.T) {
49-
// Pure null bytes embedded in otherwise valid UTF-8 text.
50-
// utf8.ValidString returns true for this, but PostgreSQL rejects 0x00 in text columns.
5141
msg := "hello\x00world"
52-
s, ok := DBSafeUTF8StringFromPtr(context.Background(), &msg)
53-
assert.True(t, ok)
54-
assert.Equal(t, "68656c6c6f00776f726c64", s)
42+
assert.Equal(t, "68656c6c6f00776f726c64", DBSafeUTF8StringFromPtr(context.Background(), &msg))
5543
}

0 commit comments

Comments
 (0)