Skip to content

Commit 9e08681

Browse files
committed
Fix pointer accessing local variable out of scope
The C11 standard states that the value of a pointer is undefined outside of its lifetime. Since we were initilizing the argv pointer in two conditional blocks, compilers that implement this standard were cleaning up the value and this caused invalid dereferences of the pointer. The change introduces two local variables with the same lifetime of the argv pointer and assigns these in the conditional blocks instead. Signed-off-by: Evan Lezar <[email protected]>
1 parent caf057b commit 9e08681

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/nvc_ldcache.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,18 +472,30 @@ nvc_ldcache_update(struct nvc_context *ctx, const struct nvc_container *cnt)
472472
if (validate_args(ctx, cnt != NULL) < 0)
473473
return (-1);
474474

475+
/*
476+
* The C11 standard states that the value of a pointer is undefined
477+
* outside of its lifetime. Since we were initilizing the argv pointer
478+
* in two conditional blocks below, compilers that implement this
479+
* standard clean up the local value and this causes invalid
480+
* dereferences of the pointer when it is used later.
481+
*
482+
* See https://github.com/NVIDIA/libnvidia-container/issues/316 for an
483+
* in-depth investigation.
484+
*/
485+
char *argv_default[] = {cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
486+
char *argv_with_compat_dir[] = {cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cuda_compat_dir, cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
475487
if ((cnt->flags & OPT_CUDA_COMPAT_MODE_LDCONFIG) && (cnt->cuda_compat_dir != NULL)) {
476488
/*
477489
* We include the cuda_compat_dir directory on the ldconfig
478490
* command line. This ensures that the CUDA Forward compat
479491
* libraries take precendence over the user-mode driver
480492
* libraries in the standard library paths (libs_dir and
481493
* libs32_dir).
482-
* */
494+
*/
483495
log_info("prefering CUDA Forward Compatibility dir when running ldconfig");
484-
argv = (char * []){cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cuda_compat_dir, cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
496+
argv = argv_with_compat_dir;
485497
} else {
486-
argv = (char * []){cnt->cfg.ldconfig, "-f", "/etc/ld.so.conf", "-C", "/etc/ld.so.cache", cnt->cfg.libs_dir, cnt->cfg.libs32_dir, NULL};
498+
argv = argv_default;
487499
}
488500

489501
if (*argv[0] == '@') {

0 commit comments

Comments
 (0)