Skip to content

Fix cli vulnerabilities found by coverity #14945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ydb/library/benchmarks/gen/tpcds-dbgen/genrand.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ genrand_integer (int *dest, int dist, int min, int max, int mean, int stream)
break;
default:
INTERNAL ("Undefined distribution");
break;
exit(EXIT_FAILURE);
}

if (dest == NULL)
Expand Down Expand Up @@ -236,7 +236,7 @@ genrand_key (ds_key_t * dest, int dist, ds_key_t min, ds_key_t max,
break;
default:
INTERNAL ("Undefined distribution");
break;
exit(EXIT_FAILURE);
}

if (dest == NULL)
Expand Down Expand Up @@ -296,7 +296,7 @@ genrand_decimal (decimal_t * dest, int dist, decimal_t * min, decimal_t * max,
break;
default:
INTERNAL ("Undefined distribution");
break;
exit(EXIT_FAILURE);
}

dest->number = res.number;
Expand Down
5 changes: 5 additions & 0 deletions ydb/library/benchmarks/gen/tpcds-dbgen/scd.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ setSCDKeys(int nColumnID, ds_key_t kIndex, char *szBKey, ds_key_t *pkBeginDateKe
}

nTableID = getTableFromColumn(nColumnID);
// Prevent array overflow. Fixing coverity issue OVERRUN
if (nTableID < 0 || nTableID >= MAX_TABLE) {
INTERNAL("Array arBKeys overflow");
exit(EXIT_FAILURE);
}
nModulo = (int)(kIndex % 6);
switch(nModulo)
{
Expand Down
16 changes: 14 additions & 2 deletions ydb/library/benchmarks/gen/tpcds-dbgen/tdefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ GetRowcountByName(char *szName)
int nTable = -1;

nTable = GetTableNumber(szName);
if (nTable >= 0)
if (nTable > 0)
return(get_rowcount(nTable - 1));

nTable = distsize(szName);
Expand Down Expand Up @@ -146,11 +146,22 @@ getTdefsByNumber(int nTable)
return(&w_tdefs[nTable]);
}
*/

void checkTdefsSize(int nTable) {
// Prevent array overflow. Fixing coverity issue OVERRUN
if (nTable < 0 || nTable >= (int)(sizeof(s_tdefs) / sizeof(s_tdefs[0]))) {
INTERNAL("Array s_tdefs overflow");
exit(EXIT_FAILURE);
}
}

tdef *
getSimpleTdefsByNumber(int nTable)
{
if (nTable >= S_BRAND)
if (nTable >= S_BRAND) {
checkTdefsSize(nTable - S_BRAND);
return(&s_tdefs[nTable - S_BRAND]);
}
return(&w_tdefs[nTable]);
}

Expand All @@ -159,6 +170,7 @@ getTdefsByNumber(int nTable)
{
if (is_set("UPDATE") && is_set("VALIDATE"))
{
checkTdefsSize(nTable);
if (s_tdefs[nTable].flags & FL_PASSTHRU)
{
switch(nTable + S_BRAND)
Expand Down
7 changes: 3 additions & 4 deletions ydb/library/benchmarks/gen/tpcds-dbgen/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,13 @@ gen_text(char *dest, int min, int max, int stream)

used_space = 0;
genrand_integer(&target_len, DIST_UNIFORM, min, max, 0, stream);
if (dest)
*dest = '\0';
else
if (!dest)
{
dest = (char *)malloc((max + 1) * sizeof(char));
MALLOC_CHECK(dest);
}

// Prevent using uninitialized *dest. Fixing coverity issue UNINIT
*dest = '\0';

while (target_len > 0)
{
Expand Down
8 changes: 8 additions & 0 deletions ydb/library/benchmarks/gen/tpcds-dbgen/w_datetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ mk_w_date (void * row, ds_key_t index)
r->d_fy_year = r->d_year;
r->d_fy_quarter_seq = r->d_quarter_seq;
r->d_fy_week_seq = r->d_week_seq;
if (r->d_dow >= MAXINT) {
INTERNAL("Int overflow for d_dow");
exit(EXIT_FAILURE);
}
r->d_day_name = weekday_names[r->d_dow + 1];
dist_member (&r->d_holiday, "calendar", day_index, 8);
if ((r->d_dow == 5) || (r->d_dow == 6))
Expand Down Expand Up @@ -290,6 +294,10 @@ vld_w_date(int nTable, ds_key_t kRow, int *Permutation)
r->d_fy_year = r->d_year;
r->d_fy_quarter_seq = r->d_quarter_seq;
r->d_fy_week_seq = r->d_week_seq;
if (r->d_dow >= MAXINT) {
INTERNAL("Int overflow for d_dow");
exit(EXIT_FAILURE);
}
r->d_day_name = weekday_names[r->d_dow + 1];
dist_member (&r->d_holiday, "calendar", day_index, 8);
if ((r->d_dow == 5) || (r->d_dow == 6))
Expand Down
9 changes: 9 additions & 0 deletions ydb/library/benchmarks/gen/tpcds-dbgen/w_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
struct W_ITEM_TBL g_w_item,
g_OldValues;

void validate_string(char *szString, unsigned long maxSize) {
// Prevent passing unterminated string. Fixing coverity issue STRING_NULL
if (strlen(szString) > maxSize) {
INTERNAL("Trying po pass unterminated string");
exit(EXIT_FAILURE);
}
}

/*
* mk_item
*/
Expand Down Expand Up @@ -190,6 +198,7 @@ mk_w_item (void* row, ds_key_t index)

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

pick_distribution (&r->i_color, "colors", 1, 2, I_COLOR);
Expand Down
4 changes: 2 additions & 2 deletions ydb/library/benchmarks/gen/tpch-dbgen/bm_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ long weight,

if (d_path == NULL)
{
sprintf(line, "%s%c%s",
snprintf(line, 256, "%s%c%s",
env_config(CONFIG_TAG, CONFIG_DFLT), PATH_SEP, path);
fp = fopen(line, "r");
OPEN_CHECK(fp, line);
Expand Down Expand Up @@ -389,7 +389,7 @@ tbl_open(int tbl, char *mode)
if (*tdefs[tbl].name == PATH_SEP)
strcpy(fullpath, tdefs[tbl].name);
else
sprintf(fullpath, "%s%c%s",
snprintf(fullpath, 256, "%s%c%s",
env_config(PATH_TAG, PATH_DFLT), PATH_SEP, tdefs[tbl].name);

retcode = stat(fullpath, &fstats);
Expand Down
2 changes: 1 addition & 1 deletion ydb/library/benchmarks/gen/tpch-dbgen/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ print_prep(int table, int update)
}
else
{
sprintf(upath, "%s%c%s.u%d",
snprintf(upath, 128, "%s%c%s.u%d",
env_config(PATH_TAG, PATH_DFLT),
PATH_SEP, tdefs[table].name, update);
}
Expand Down
3 changes: 3 additions & 0 deletions ydb/library/benchmarks/gen/tpch-dbgen/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ dbg_text(char *tgt, int min, int max, int sd)
}
else /* chop the new sentence off to match the length target */
{
if (needed >= (DSS_HUGE)sizeof(sentence)) {
INTERNAL_ERROR("Array sentence overflow");
}
sentence[needed] = '\0';
strcpy(cp, sentence);
wordlen += needed;
Expand Down
4 changes: 2 additions & 2 deletions ydb/library/workload/tpcds/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ extern "C" int dist_op(void *dest, int op, char *d_name, int vset, int wset, int
if (dest == NULL) {
dest = (date_t *)malloc(sizeof(date_t));
}
strtodt(*(date_t **)dest, char_val);
strtodt((date_t *)dest, char_val);
break;
case TKN_DECIMAL:
if (dest == NULL) {
dest = (decimal_t *)malloc(sizeof(decimal_t));
}
strtodec(*(decimal_t **)dest,char_val);
strtodec((decimal_t *)dest,char_val);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions ydb/public/lib/ydb_cli/commands/ydb_workload_import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ class TWorkloadCommandImport::TUploadCommand::TFileWriter: public IWriter {
if (auto* result = MapFindPtr(CsvOutputs, fname)) {
return std::make_pair(result->Get(), false);
}
auto result = MakeAtomicShared<TFileOutput>(Owner.UploadParams.FileOutputPath / fname);
CsvOutputs[fname] = result;
auto& result = CsvOutputs[fname];
result = MakeAtomicShared<TFileOutput>(Owner.UploadParams.FileOutputPath / fname);
return std::make_pair(result.Get(), true);
}
TMap<TString, TAtomicSharedPtr<TFileOutput>> CsvOutputs;
Expand Down
2 changes: 1 addition & 1 deletion ydb/public/lib/ydb_cli/dump/restore_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class IDataAccumulator;
class TBatch {
TStringBuilder Data;
TVector<TLocation> Locations;
IDataAccumulator* OriginAccumulator;
IDataAccumulator* OriginAccumulator = nullptr;

public:
void Add(const TLine& line);
Expand Down
Loading