Skip to content
Merged
Changes from 2 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
10 changes: 5 additions & 5 deletions Modules/_zstd/zstddict.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,
/* All dictionaries must be at least 8 bytes */
if (dict_content->len < 8) {
PyErr_SetString(PyExc_ValueError,
"Zstandard dictionary content should at least "
"8 bytes.");
goto error;
"Zstandard dictionary content must be longer "
"than eight bytes.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can also be exactly 8 bytes, right?

What happens if you remove this check? What error will you get?

Copy link
Member Author

@AA-Turner AA-Turner May 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wording error, sorry.

What happens if you remove this check? What error will you get?

From RFC 8878 s.5, a dictionary can either be in Zstandard format or 'raw content' format.

  • "raw content" dictionaries ... must be at least 8 bytes.

  • The Zstandard format has a required 4 byte magic number and 4 byte ID

This check only bites when is_raw is True, as normally we check that the dict is a valid Zstandard directory via ZSTD_getDictID_fromDict() != 0. That function returns 0 if dictSize < 8 or otherwise if the data does not match the Zstandard header.

The Zstandard documentation is quite sparse on 'raw content' dictionaries, but I would conclude that the early error here probably doesn't hurt, and helps ensure we are conformant to the RFC.

return NULL;
}

ZstdDict* self = PyObject_GC_New(ZstdDict, type);
Expand All @@ -72,8 +72,8 @@ _zstd_ZstdDict_new_impl(PyTypeObject *type, Py_buffer *dict_content,

self->dict_buffer = PyMem_Malloc(dict_content->len);
if (!self->dict_buffer) {
Py_DECREF(self);
return PyErr_NoMemory();
PyErr_NoMemory();
goto error;
}
memcpy(self->dict_buffer, dict_content->buf, dict_content->len);
self->dict_len = dict_content->len;
Expand Down
Loading