hpyproject / hpy

HPy: a better API for Python
https://hpyproject.org
MIT License
1.02k stars 52 forks source link

debug ctx_Type_GetName does not take trailing NULL into account #448

Closed mattip closed 1 year ago

mattip commented 1 year ago

In this call the trailing NULL is not copied back out of protect_and_associate_data_ptr, so I am getting garbage on the end of the returned char*. I think n_name should be incremented to include the trailing NULL. https://github.com/hpyproject/hpy/blob/8310a762d78e3412464b1869959a77da013e6307/hpy/debug/src/debug_ctx.c#L559-L564

mattip commented 1 year ago

I put this fix into PyPy:

# HG changeset patch
# User Matti Picus <matti.picus@gmail.com>
# Date 1694719428 -10800
#      Thu Sep 14 22:23:48 2023 +0300
# Branch hpy-0.9
# Node ID d1d83b7bc8d3e18708d15c4dfbc802cd3a640552
# Parent  93f6b1f6f9d6fe6cfa837b231e61fc70c66218c5
Fix for upstream issue 448: copy out trailing NULL

diff -r 93f6b1f6f9d6 -r d1d83b7bc8d3 pypy/module/_hpy_universal/_vendored/hpy/debug/src/debug_ctx.c
--- a/pypy/module/_hpy_universal/_vendored/hpy/debug/src/debug_ctx.c    Thu Sep 14 22:23:09 2023 +0300
+++ b/pypy/module/_hpy_universal/_vendored/hpy/debug/src/debug_ctx.c    Thu Sep 14 22:23:48 2023 +0300
@@ -559,7 +559,7 @@
     ctx_info->is_valid = false;
     const char *name = HPyType_GetName(uctx, uh_type);
     ctx_info->is_valid = true;
-    n_name = strlen(name);
+    n_name = strlen(name) + 1;
     return (const char *)protect_and_associate_data_ptr(type, (void *)name, n_name);
 }
fangerer commented 1 year ago

Thanks for the catch. I'll include it in the final 0.9.0 release.