Skip to content

Commit f5841df

Browse files
Merge pull request #1373 from lplewa/strcpy
add missing \0 in case of strcpy() too long string
2 parents 94bc4ae + 98ac983 commit f5841df

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/pool/pool_disjoint.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ static int CTL_READ_HANDLER(name)(void *ctx, umf_ctl_query_source_t source,
4545
return -1;
4646
}
4747

48-
strncpy((char *)arg, pool->params.name, size);
48+
if (size > 0) {
49+
strncpy((char *)arg, pool->params.name, size - 1);
50+
((char *)arg)[size - 1] = '\0';
51+
}
4952
return 0;
5053
}
5154

@@ -63,6 +66,7 @@ static int CTL_WRITE_HANDLER(name)(void *ctx, umf_ctl_query_source_t source,
6366
}
6467

6568
strncpy(pool->params.name, (char *)arg, sizeof(pool->params.name) - 1);
69+
pool->params.name[sizeof(pool->params.name) - 1] = '\0';
6670
return 0;
6771
}
6872

@@ -1089,6 +1093,7 @@ umfDisjointPoolParamsCreate(umf_disjoint_pool_params_handle_t *hParams) {
10891093
};
10901094

10911095
strncpy(params->name, DEFAULT_NAME, sizeof(params->name) - 1);
1096+
params->name[sizeof(params->name) - 1] = '\0';
10921097

10931098
*hParams = params;
10941099
return UMF_RESULT_SUCCESS;
@@ -1190,5 +1195,6 @@ umfDisjointPoolParamsSetName(umf_disjoint_pool_params_handle_t hParams,
11901195
}
11911196

11921197
strncpy(hParams->name, name, sizeof(hParams->name) - 1);
1198+
hParams->name[sizeof(hParams->name) - 1] = '\0';
11931199
return UMF_RESULT_SUCCESS;
11941200
}

src/provider/provider_cuda.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,11 @@ static void cu_memory_provider_get_last_native_error(void *provider,
530530
if (result == CUDA_SUCCESS && error_name != NULL) {
531531
strncpy(TLS_last_native_error.msg_buff, error_name,
532532
TLS_MSG_BUF_LEN - 1);
533+
TLS_last_native_error.msg_buff[TLS_MSG_BUF_LEN - 1] = '\0';
533534
} else {
534535
strncpy(TLS_last_native_error.msg_buff, "cuGetErrorName() failed",
535536
TLS_MSG_BUF_LEN - 1);
537+
TLS_last_native_error.msg_buff[TLS_MSG_BUF_LEN - 1] = '\0';
536538
}
537539

538540
buf_size = strlen(TLS_last_native_error.msg_buff);

0 commit comments

Comments
 (0)