| 4790 | */ |
| 4791 | |
| 4792 | int GDALCheckBandCount(int nBands, int bIsZeroAllowed) |
| 4793 | { |
| 4794 | if (nBands < 0 || (!bIsZeroAllowed && nBands == 0)) |
| 4795 | { |
| 4796 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid band count : %d", |
| 4797 | nBands); |
| 4798 | return FALSE; |
| 4799 | } |
| 4800 | const char *pszMaxBandCount = |
| 4801 | CPLGetConfigOption("GDAL_MAX_BAND_COUNT", "65536"); |
| 4802 | int nMaxBands = std::clamp(atoi(pszMaxBandCount), 0, INT_MAX - 1); |
| 4803 | if (nBands > nMaxBands) |
| 4804 | { |
| 4805 | CPLError(CE_Failure, CPLE_AppDefined, |
| 4806 | "Invalid band count : %d. Maximum allowed currently is %d. " |
| 4807 | "Define GDAL_MAX_BAND_COUNT to a higher level if it is a " |
| 4808 | "legitimate number.", |
| 4809 | nBands, nMaxBands); |
| 4810 | return FALSE; |
| 4811 | } |
| 4812 | return TRUE; |
| 4813 | } |
| 4814 | |
| 4815 | CPL_C_END |
| 4816 |
no test coverage detected