{"record":{"id":"c0c10992193f7c8b","repo":"siyuan-note/siyuan","slug":"invalid-bazaar-rating-region","errorCode":null,"errorMessage":"invalid bazaar rating region","messagePattern":"invalid bazaar rating region","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/bazaar/rating.go","lineNumber":409,"sourceCode":"\tcache.mu.RLock()\n\tdefer cache.mu.RUnlock()\n\tret.loaded = cache.loaded\n\tfresh = cache.loaded && now.Before(cache.expiresAt)\n\tif !cache.loaded {\n\t\treturn\n\t}\n\tret.data = cloneBazaarRatingDistributions(cache.data)\n\tfor packageName, override := range cache.overrides {\n\t\tif now.Before(override.expiresAt) {\n\t\t\tret.data[packageName] = override.distribution\n\t\t}\n\t}\n\treturn\n}\n\nfunc fetchBazaarRatingRegion(ctx context.Context, region int) (ret map[string]bazaarRatingDistribution, err error) {\n\tif region < 0 || bazaarRatingRegionCount <= region {\n\t\treturn nil, errors.New(\"invalid bazaar rating region\")\n\t}\n\n\ttimeBucket := bazaarRatingNow().Unix() / int64(bazaarRatingCDNBucket/time.Second)\n\tu := fmt.Sprintf(\"%s/bazaar/ratings/v1/%s?t=%d\", bazaarRatingStatServer, bazaarRatingRegionFiles[region], timeBucket)\n\tbuf := &bytes.Buffer{}\n\tresp, err := httpclient.NewBrowserRequest().SetRetryCount(0).SetContext(ctx).SetOutput(buf).Get(u)\n\tif nil != err {\n\t\treturn nil, err\n\t}\n\tif 200 != resp.StatusCode {\n\t\treturn nil, fmt.Errorf(\"unexpected status code: %d\", resp.StatusCode)\n\t}\n\n\tret, err = parseBazaarRatingRegion(buf.Bytes())\n\treturn\n}\n\nfunc parseBazaarRatingRegion(data []byte) (ret map[string]bazaarRatingDistribution, err error) {","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/bazaar/rating.go#L391-L427","documentation":"fetchBazaarRatingRegion downloads a pre-templated CDN JSON file holding per-package rating distributions for one region bucket. The region parameter indexes bazaarRatingRegionFiles, so any region outside [0, bazaarRatingRegionCount) has no corresponding file and the function rejects it up front with this error. It is a guard against programmer error rather than a network condition.","triggerScenarios":"Calling fetchBazaarRatingRegion with a negative region, or a region >= bazaarRatingRegionCount — e.g. a caller computing the region from user input or a shuffled shard list that got out of sync with the region-count constant.","commonSituations":"A new region file was added to the CDN but bazaarRatingRegionCount/RegionFiles were not updated, so a caller requests the new index; a loop bound uses len of a differently sized collection; off-by-one when iterating regions (using <= instead of <).","solutions":["Check the region value passed in and clamp/validate it to 0 <= region < bazaarRatingRegionCount before calling","If you intended to add a region, add its filename to bazaarRatingRegionFiles and bump bazaarRatingRegionCount together","Fix loop bounds so the region index is derived with `<` not `<=`","Log the offending region value at the call site to find where the out-of-range index originates"],"exampleFix":"// before\nratings, err := fetchBazaarRatingRegion(ctx, region)\n// after\nif region < 0 || region >= bazaarRatingRegionCount {\n    return fmt.Errorf(\"region %d out of range\", region)\n}\nratings, err := fetchBazaarRatingRegion(ctx, region)","handlingStrategy":"validation","validationCode":"if region < 0 || region >= bazaarRatingRegionCount {\n    return fmt.Errorf(\"region %d out of range [0,%d)\", region, bazaarRatingRegionCount)\n}","typeGuard":null,"tryCatchPattern":"dist, err := fetchBazaarRatingRegion(ctx, region)\nif err != nil && strings.Contains(err.Error(), \"invalid bazaar rating region\") {\n    log.Errorf(\"bad region index %d, check region config/constants\", region)\n    return err\n}","preventionTips":["Derive region loop bounds from bazaarRatingRegionCount, never a hard-coded number","Keep bazaarRatingRegionFiles and bazaarRatingRegionCount in sync when adding regions","Validate user- or config-supplied region values before passing them in","Use `region < count` (not `<=`) in iteration conditions"],"tags":["bazaar","ratings","argument-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}