(t *testing.T)
| 1168 | } |
| 1169 | |
| 1170 | func TestAllowNilZeroCopy(t *testing.T) { |
| 1171 | // Test that zerocopy+allownil combination works correctly with security fixes |
| 1172 | |
| 1173 | t.Run("ZeroCopy_AllowNil_NilValues_UnmarshalMsg", func(t *testing.T) { |
| 1174 | data := AllowNilTestData{} |
| 1175 | |
| 1176 | // Create message with nil for zerocopy+allownil field |
| 1177 | buf := msgp.AppendMapHeader(nil, 1) |
| 1178 | buf = msgp.AppendString(buf, "nil_zc_slice") |
| 1179 | buf = msgp.AppendNil(buf) |
| 1180 | |
| 1181 | _, err := data.UnmarshalMsg(buf) |
| 1182 | if err != nil { |
| 1183 | t.Fatalf("UnmarshalMsg failed with zerocopy allownil nil value: %v", err) |
| 1184 | } |
| 1185 | |
| 1186 | // Field should remain nil |
| 1187 | if data.NilZCSlice != nil { |
| 1188 | t.Errorf("Expected NilZCSlice to remain nil, got %v", data.NilZCSlice) |
| 1189 | } |
| 1190 | }) |
| 1191 | |
| 1192 | t.Run("ZeroCopy_AllowNil_EmptyData_UnmarshalMsg", func(t *testing.T) { |
| 1193 | data := AllowNilTestData{} |
| 1194 | |
| 1195 | // Create message with empty data for zerocopy+allownil field |
| 1196 | buf := msgp.AppendMapHeader(nil, 1) |
| 1197 | buf = msgp.AppendString(buf, "nil_zc_slice") |
| 1198 | buf = msgp.AppendBytes(buf, []byte{}) |
| 1199 | |
| 1200 | _, err := data.UnmarshalMsg(buf) |
| 1201 | if err != nil { |
| 1202 | t.Fatalf("UnmarshalMsg failed with zerocopy allownil empty data: %v", err) |
| 1203 | } |
| 1204 | |
| 1205 | // Field should be allocated but empty |
| 1206 | if data.NilZCSlice == nil { |
| 1207 | t.Error("Expected NilZCSlice to be allocated (empty), got nil") |
| 1208 | } |
| 1209 | if len(data.NilZCSlice) != 0 { |
| 1210 | t.Errorf("Expected NilZCSlice to be empty, got length %d", len(data.NilZCSlice)) |
| 1211 | } |
| 1212 | }) |
| 1213 | |
| 1214 | t.Run("ZeroCopy_AllowNil_WithData_UnmarshalMsg", func(t *testing.T) { |
| 1215 | data := AllowNilTestData{} |
| 1216 | testData := []byte{1, 2, 3, 4, 5} |
| 1217 | |
| 1218 | // Create message with actual data for zerocopy+allownil field |
| 1219 | buf := msgp.AppendMapHeader(nil, 1) |
| 1220 | buf = msgp.AppendString(buf, "nil_zc_slice") |
| 1221 | buf = msgp.AppendBytes(buf, testData) |
| 1222 | |
| 1223 | originalBuf := make([]byte, len(buf)) |
| 1224 | copy(originalBuf, buf) |
| 1225 | |
| 1226 | _, err := data.UnmarshalMsg(buf) |
| 1227 | if err != nil { |
nothing calls this directly
no test coverage detected
searching dependent graphs…