Skip to content

Commit 9029d02

Browse files
authored
Fix cli vulnerabilities found by coverity (#14945)
1 parent b58a028 commit 9029d02

File tree

12 files changed

+53
-17
lines changed

12 files changed

+53
-17
lines changed

ydb/library/benchmarks/gen/tpcds-dbgen/genrand.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ genrand_integer (int *dest, int dist, int min, int max, int mean, int stream)
189189
break;
190190
default:
191191
INTERNAL ("Undefined distribution");
192-
break;
192+
exit(EXIT_FAILURE);
193193
}
194194

195195
if (dest == NULL)
@@ -236,7 +236,7 @@ genrand_key (ds_key_t * dest, int dist, ds_key_t min, ds_key_t max,
236236
break;
237237
default:
238238
INTERNAL ("Undefined distribution");
239-
break;
239+
exit(EXIT_FAILURE);
240240
}
241241

242242
if (dest == NULL)
@@ -296,7 +296,7 @@ genrand_decimal (decimal_t * dest, int dist, decimal_t * min, decimal_t * max,
296296
break;
297297
default:
298298
INTERNAL ("Undefined distribution");
299-
break;
299+
exit(EXIT_FAILURE);
300300
}
301301

302302
dest->number = res.number;

ydb/library/benchmarks/gen/tpcds-dbgen/scd.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ setSCDKeys(int nColumnID, ds_key_t kIndex, char *szBKey, ds_key_t *pkBeginDateKe
9696
}
9797

9898
nTableID = getTableFromColumn(nColumnID);
99+
// Prevent array overflow. Fixing coverity issue OVERRUN
100+
if (nTableID < 0 || nTableID >= MAX_TABLE) {
101+
INTERNAL("Array arBKeys overflow");
102+
exit(EXIT_FAILURE);
103+
}
99104
nModulo = (int)(kIndex % 6);
100105
switch(nModulo)
101106
{

ydb/library/benchmarks/gen/tpcds-dbgen/tdefs.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ GetRowcountByName(char *szName)
7171
int nTable = -1;
7272

7373
nTable = GetTableNumber(szName);
74-
if (nTable >= 0)
74+
if (nTable > 0)
7575
return(get_rowcount(nTable - 1));
7676

7777
nTable = distsize(szName);
@@ -146,11 +146,22 @@ getTdefsByNumber(int nTable)
146146
return(&w_tdefs[nTable]);
147147
}
148148
*/
149+
150+
void checkTdefsSize(int nTable) {
151+
// Prevent array overflow. Fixing coverity issue OVERRUN
152+
if (nTable < 0 || nTable >= (int)(sizeof(s_tdefs) / sizeof(s_tdefs[0]))) {
153+
INTERNAL("Array s_tdefs overflow");
154+
exit(EXIT_FAILURE);
155+
}
156+
}
157+
149158
tdef *
150159
getSimpleTdefsByNumber(int nTable)
151160
{
152-
if (nTable >= S_BRAND)
161+
if (nTable >= S_BRAND) {
162+
checkTdefsSize(nTable - S_BRAND);
153163
return(&s_tdefs[nTable - S_BRAND]);
164+
}
154165
return(&w_tdefs[nTable]);
155166
}
156167

@@ -159,6 +170,7 @@ getTdefsByNumber(int nTable)
159170
{
160171
if (is_set("UPDATE") && is_set("VALIDATE"))
161172
{
173+
checkTdefsSize(nTable);
162174
if (s_tdefs[nTable].flags & FL_PASSTHRU)
163175
{
164176
switch(nTable + S_BRAND)

ydb/library/benchmarks/gen/tpcds-dbgen/text.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,13 @@ gen_text(char *dest, int min, int max, int stream)
160160

161161
used_space = 0;
162162
genrand_integer(&target_len, DIST_UNIFORM, min, max, 0, stream);
163-
if (dest)
164-
*dest = '\0';
165-
else
163+
if (!dest)
166164
{
167165
dest = (char *)malloc((max + 1) * sizeof(char));
168166
MALLOC_CHECK(dest);
169167
}
170-
168+
// Prevent using uninitialized *dest. Fixing coverity issue UNINIT
169+
*dest = '\0';
171170

172171
while (target_len > 0)
173172
{

ydb/library/benchmarks/gen/tpcds-dbgen/w_datetbl.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ mk_w_date (void * row, ds_key_t index)
113113
r->d_fy_year = r->d_year;
114114
r->d_fy_quarter_seq = r->d_quarter_seq;
115115
r->d_fy_week_seq = r->d_week_seq;
116+
if (r->d_dow >= MAXINT) {
117+
INTERNAL("Int overflow for d_dow");
118+
exit(EXIT_FAILURE);
119+
}
116120
r->d_day_name = weekday_names[r->d_dow + 1];
117121
dist_member (&r->d_holiday, "calendar", day_index, 8);
118122
if ((r->d_dow == 5) || (r->d_dow == 6))
@@ -290,6 +294,10 @@ vld_w_date(int nTable, ds_key_t kRow, int *Permutation)
290294
r->d_fy_year = r->d_year;
291295
r->d_fy_quarter_seq = r->d_quarter_seq;
292296
r->d_fy_week_seq = r->d_week_seq;
297+
if (r->d_dow >= MAXINT) {
298+
INTERNAL("Int overflow for d_dow");
299+
exit(EXIT_FAILURE);
300+
}
293301
r->d_day_name = weekday_names[r->d_dow + 1];
294302
dist_member (&r->d_holiday, "calendar", day_index, 8);
295303
if ((r->d_dow == 5) || (r->d_dow == 6))

ydb/library/benchmarks/gen/tpcds-dbgen/w_item.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@
5858
struct W_ITEM_TBL g_w_item,
5959
g_OldValues;
6060

61+
void validate_string(char *szString, unsigned long maxSize) {
62+
// Prevent passing unterminated string. Fixing coverity issue STRING_NULL
63+
if (strlen(szString) > maxSize) {
64+
INTERNAL("Trying po pass unterminated string");
65+
exit(EXIT_FAILURE);
66+
}
67+
}
68+
6169
/*
6270
* mk_item
6371
*/
@@ -190,6 +198,7 @@ mk_w_item (void* row, ds_key_t index)
190198

191199
gen_charset(r->i_formulation, DIGITS, RS_I_FORMULATION, RS_I_FORMULATION, I_FORMULATION);
192200
embed_string(r->i_formulation, "colors", 1, 2, I_FORMULATION);
201+
validate_string(r->i_formulation, RS_I_FORMULATION);
193202
changeSCD(SCD_CHAR, &r->i_formulation, &rOldValues->i_formulation, &nFieldChangeFlags, bFirstRecord);
194203

195204
pick_distribution (&r->i_color, "colors", 1, 2, I_COLOR);

ydb/library/benchmarks/gen/tpch-dbgen/bm_utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ long weight,
302302

303303
if (d_path == NULL)
304304
{
305-
sprintf(line, "%s%c%s",
305+
snprintf(line, 256, "%s%c%s",
306306
env_config(CONFIG_TAG, CONFIG_DFLT), PATH_SEP, path);
307307
fp = fopen(line, "r");
308308
OPEN_CHECK(fp, line);
@@ -389,7 +389,7 @@ tbl_open(int tbl, char *mode)
389389
if (*tdefs[tbl].name == PATH_SEP)
390390
strcpy(fullpath, tdefs[tbl].name);
391391
else
392-
sprintf(fullpath, "%s%c%s",
392+
snprintf(fullpath, 256, "%s%c%s",
393393
env_config(PATH_TAG, PATH_DFLT), PATH_SEP, tdefs[tbl].name);
394394

395395
retcode = stat(fullpath, &fstats);

ydb/library/benchmarks/gen/tpch-dbgen/print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ print_prep(int table, int update)
7575
}
7676
else
7777
{
78-
sprintf(upath, "%s%c%s.u%d",
78+
snprintf(upath, 128, "%s%c%s.u%d",
7979
env_config(PATH_TAG, PATH_DFLT),
8080
PATH_SEP, tdefs[table].name, update);
8181
}

ydb/library/benchmarks/gen/tpch-dbgen/text.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ dbg_text(char *tgt, int min, int max, int sd)
323323
}
324324
else /* chop the new sentence off to match the length target */
325325
{
326+
if (needed >= (DSS_HUGE)sizeof(sentence)) {
327+
INTERNAL_ERROR("Array sentence overflow");
328+
}
326329
sentence[needed] = '\0';
327330
strcpy(cp, sentence);
328331
wordlen += needed;

ydb/library/workload/tpcds/driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,13 @@ extern "C" int dist_op(void *dest, int op, char *d_name, int vset, int wset, int
224224
if (dest == NULL) {
225225
dest = (date_t *)malloc(sizeof(date_t));
226226
}
227-
strtodt(*(date_t **)dest, char_val);
227+
strtodt((date_t *)dest, char_val);
228228
break;
229229
case TKN_DECIMAL:
230230
if (dest == NULL) {
231231
dest = (decimal_t *)malloc(sizeof(decimal_t));
232232
}
233-
strtodec(*(decimal_t **)dest,char_val);
233+
strtodec((decimal_t *)dest,char_val);
234234
break;
235235
}
236236

ydb/public/lib/ydb_cli/commands/ydb_workload_import.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ class TWorkloadCommandImport::TUploadCommand::TFileWriter: public IWriter {
226226
if (auto* result = MapFindPtr(CsvOutputs, fname)) {
227227
return std::make_pair(result->Get(), false);
228228
}
229-
auto result = MakeAtomicShared<TFileOutput>(Owner.UploadParams.FileOutputPath / fname);
230-
CsvOutputs[fname] = result;
229+
auto& result = CsvOutputs[fname];
230+
result = MakeAtomicShared<TFileOutput>(Owner.UploadParams.FileOutputPath / fname);
231231
return std::make_pair(result.Get(), true);
232232
}
233233
TMap<TString, TAtomicSharedPtr<TFileOutput>> CsvOutputs;

ydb/public/lib/ydb_cli/dump/restore_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class IDataAccumulator;
7272
class TBatch {
7373
TStringBuilder Data;
7474
TVector<TLocation> Locations;
75-
IDataAccumulator* OriginAccumulator;
75+
IDataAccumulator* OriginAccumulator = nullptr;
7676

7777
public:
7878
void Add(const TLine& line);

0 commit comments

Comments
 (0)