Skip to content

Commit cfb9527

Browse files
further refactoring
1 parent 1675cd0 commit cfb9527

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

ydb/library/yql/providers/s3/compressors/brotli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool TReadBuffer::nextImpl() {
7676
ythrow TCodeLineException(TIssuesIds::KIKIMR_BAD_REQUEST) << "Brotli decoder failed to decompress buffer: "
7777
<< BrotliDecoderErrorString(BrotliDecoderGetErrorCode(DecoderState_));
7878
case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
79-
YQL_ENSURE(availableOut != OutBuffer.size(), "Buffer passed to read in Brotli decoder is too small");
79+
YQL_ENSURE_CODELINE(availableOut != OutBuffer.size(),TIssuesIds::KIKIMR_BAD_REQUEST, "Buffer passed to read in Brotli decoder is too small");
8080
break;
8181
default:
8282
break;

ydb/library/yql/providers/s3/compressors/gz.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class TCompressor : public TOutputQueue<> {
116116
Z_.avail_out = OutputBufferSize;
117117

118118
const auto code = deflate(&Z_, done ? Z_FINISH : Z_BLOCK);
119-
YQL_ENSURE((done ? Z_STREAM_END : Z_OK) == code, "code: " << code << ", error: " << GetErrMsg(Z_));
119+
YQL_ENSURE_CODELINE((done ? Z_STREAM_END : Z_OK) == code, TIssuesIds::KIKIMR_BAD_REQUEST, "code: " << code << ", error: " << GetErrMsg(Z_));
120120

121121
if (const auto size = OutputBufferSize - Z_.avail_out)
122122
TOutputQueue::Push(TString(OutputBuffer.get(), size));

ydb/library/yql/providers/s3/compressors/lz4io.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <contrib/libs/lz4/lz4hc.h>
88

99
#include <ydb/library/yql/utils/yql_panic.h>
10+
#include <ydb/library/yql/utils/exceptions.h>
11+
#include <ydb/library/yql/core/issue/protos/issue_id.pb.h>
1012
#include "output_queue_impl.h"
1113

1214
namespace NYql {
@@ -53,7 +55,7 @@ EStreamType CheckMagic(const void* data) {
5355

5456
EStreamType CheckMagic(NDB::ReadBuffer& input) {
5557
char data[4u];
56-
YQL_ENSURE(input.read(data, sizeof(data)) == sizeof(data), "Buffer too small.");
58+
YQL_ENSURE_CODELINE(input.read(data, sizeof(data)) == sizeof(data), TIssuesIds::KIKIMR_BAD_REQUEST, "Buffer too small.");
5759
return CheckMagic(data);
5860
}
5961

@@ -62,7 +64,7 @@ EStreamType CheckMagic(NDB::ReadBuffer& input) {
6264
TReadBuffer::TReadBuffer(NDB::ReadBuffer& source)
6365
: NDB::ReadBuffer(nullptr, 0ULL), Source(source), StreamType(CheckMagic(Source)), Pos(0ULL), Remaining(0ULL)
6466
{
65-
YQL_ENSURE(StreamType != EStreamType::Unknown, "Wrong magic.");
67+
YQL_ENSURE_CODELINE(StreamType != EStreamType::Unknown, TIssuesIds::KIKIMR_BAD_REQUEST, "Wrong magic.");
6668
if (StreamType == EStreamType::Frame) {
6769
const auto errorCode = LZ4F_createDecompressionContext(&Ctx, LZ4F_VERSION);
6870
YQL_ENSURE(!LZ4F_isError(errorCode), "Can't create LZ4F context resource: " << LZ4F_getErrorName(errorCode));
@@ -75,7 +77,7 @@ TReadBuffer::TReadBuffer(NDB::ReadBuffer& source)
7577
WriteLE32(InBuffer.data(), Lz4ioMagicNumber);
7678

7779
NextToLoad = LZ4F_decompress_usingDict(Ctx, OutBuffer.data(), &outSize, InBuffer.data(), &inSize, nullptr, 0ULL, nullptr);
78-
YQL_ENSURE(!LZ4F_isError(NextToLoad), "Header error: " << LZ4F_getErrorName(NextToLoad));
80+
YQL_ENSURE_CODELINE(!LZ4F_isError(NextToLoad), TIssuesIds::KIKIMR_BAD_REQUEST, "Header error: " << LZ4F_getErrorName(NextToLoad));
7981
}
8082
}
8183

@@ -120,7 +122,7 @@ size_t TReadBuffer::DecompressFrame() {
120122
if (Pos >= Remaining) {
121123
for (auto toRead = NextToLoad; toRead > 0U;) {
122124
const auto sizeCheck = Source.read(InBuffer.data() + NextToLoad - toRead, toRead);
123-
YQL_ENSURE(sizeCheck > 0U && sizeCheck <= toRead, "Cannot access compressed block.");
125+
YQL_ENSURE_CODELINE(sizeCheck > 0U && sizeCheck <= toRead, TIssuesIds::KIKIMR_BAD_REQUEST, "Cannot access compressed block.");
124126
toRead -= sizeCheck;
125127
}
126128

@@ -132,7 +134,7 @@ size_t TReadBuffer::DecompressFrame() {
132134
while (Pos < Remaining || (decodedBytes == OutBuffer.size())) {
133135
decodedBytes = OutBuffer.size();
134136
NextToLoad = LZ4F_decompress_usingDict(Ctx, OutBuffer.data(), &decodedBytes, InBuffer.data() + Pos, &Remaining, nullptr, 0ULL, nullptr);
135-
YQL_ENSURE(!LZ4F_isError(NextToLoad), "Decompression error: " << LZ4F_getErrorName(NextToLoad));
137+
YQL_ENSURE_CODELINE(!LZ4F_isError(NextToLoad), TIssuesIds::KIKIMR_BAD_REQUEST, "Decompression error: " << LZ4F_getErrorName(NextToLoad));
136138
Pos += Remaining;
137139

138140
if (decodedBytes)
@@ -153,21 +155,21 @@ size_t TReadBuffer::DecompressLegacy() {
153155
unsigned int blockSize = 0U;
154156

155157
if (const auto sizeCheck = Source.read(InBuffer.data(), 4U)) {
156-
YQL_ENSURE(sizeCheck == 4U, "Cannot access block size.");
158+
YQL_ENSURE_CODELINE(sizeCheck == 4U, TIssuesIds::KIKIMR_BAD_REQUEST, "Cannot access block size.");
157159
blockSize = ReadLE32(InBuffer.data());
158-
YQL_ENSURE(blockSize <= LZ4_COMPRESSBOUND(LegacyBlockSize), "Block size out of bounds.");
160+
YQL_ENSURE_CODELINE(blockSize <= LZ4_COMPRESSBOUND(LegacyBlockSize), TIssuesIds::KIKIMR_BAD_REQUEST, "Block size out of bounds.");
159161
} else
160162
return 0ULL;
161163

162164
for (auto toRead = blockSize; toRead > 0U;) {
163165
const auto sizeCheck = Source.read(InBuffer.data() + blockSize - toRead, toRead);
164-
YQL_ENSURE(sizeCheck > 0U && sizeCheck <= toRead, "Cannot access compressed block.");
166+
YQL_ENSURE_CODELINE(sizeCheck > 0U && sizeCheck <= toRead, TIssuesIds::KIKIMR_BAD_REQUEST, "Cannot access compressed block.");
165167
toRead -= sizeCheck;
166168
}
167169

168170
const auto decodeSize = LZ4_decompress_safe(InBuffer.data(), OutBuffer.data(), (int)blockSize, LegacyBlockSize);
169171

170-
YQL_ENSURE(decodeSize >= 0, "Corrupted input detected.");
172+
YQL_ENSURE_CODELINE(decodeSize >= 0, TIssuesIds::KIKIMR_BAD_REQUEST, "Corrupted input detected.");
171173
return size_t(decodeSize);
172174
}
173175

ydb/library/yql/providers/s3/compressors/zstd.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <util/generic/size_literals.h>
44
#include <ydb/library/yql/utils/yql_panic.h>
5+
#include <ydb/library/yql/utils/exceptions.h>
6+
#include <ydb/library/yql/core/issue/protos/issue_id.pb.h>
57
#include "output_queue_impl.h"
68

79
namespace NYql {
@@ -34,13 +36,13 @@ bool TReadBuffer::nextImpl() {
3436
zIn.pos = Offset_ = 0;
3537
if (!zIn.size) {
3638
// end of stream, need to check that there is no uncompleted blocks
37-
YQL_ENSURE(!returnCode, "Incomplete block.");
39+
YQL_ENSURE_CODELINE(!returnCode, TIssuesIds::KIKIMR_BAD_REQUEST, "Incomplete block.");
3840
Finished_ = true;
3941
break;
4042
}
4143
}
4244
returnCode = ::ZSTD_decompressStream(ZCtx_, &zOut, &zIn);
43-
YQL_ENSURE(!::ZSTD_isError(returnCode), "Decompress failed: " << ::ZSTD_getErrorName(returnCode));
45+
YQL_ENSURE_CODELINE(!::ZSTD_isError(returnCode), TIssuesIds::KIKIMR_BAD_REQUEST, "Decompress failed: " << ::ZSTD_getErrorName(returnCode));
4446
if (!returnCode) {
4547
// The frame is over, prepare to (maybe) start a new frame
4648
::ZSTD_initDStream(ZCtx_);
@@ -65,7 +67,7 @@ class TCompressor : public TOutputQueue<> {
6567
: ZCtx_(::ZSTD_createCStream())
6668
{
6769
const auto ret = ::ZSTD_initCStream(ZCtx_, level);
68-
YQL_ENSURE(!::ZSTD_isError(ret), "code: " << ret << ", error: " << ::ZSTD_getErrorName(ret));
70+
YQL_ENSURE_CODELINE(!::ZSTD_isError(ret), TIssuesIds::KIKIMR_BAD_REQUEST, "code: " << ret << ", error: " << ::ZSTD_getErrorName(ret));
6971
}
7072

7173
~TCompressor() {

ydb/library/yql/utils/exceptions.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ struct TCodeLineException: public yexception {
2424

2525
TCodeLineException operator+(const TSourceLocation& sl, TCodeLineException&& t);
2626

27-
} // namespace NFq
27+
#define YQL_ENSURE_CODELINE(CONDITION, CODE, ...) \
28+
do { \
29+
if (Y_UNLIKELY(!(CONDITION))) { \
30+
ythrow TCodeLineException(CODE) << __VA_ARGS__; \
31+
} \
32+
} while (0)
33+
34+
} // namespace NYql

0 commit comments

Comments
 (0)