From 7ff0129f36c5a67178141ab318fb6f81156444d0 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 29 Nov 2008 22:43:30 -0600 Subject: [PATCH] Fixed ReadFileString ignoring fourth parameter (bug 3459, r=ds). --- core/smn_filesystem.cpp | 22 ++++++++++++++++++++-- plugins/include/files.inc | 10 +++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/smn_filesystem.cpp b/core/smn_filesystem.cpp index d178b8ce..935f7cdf 100644 --- a/core/smn_filesystem.cpp +++ b/core/smn_filesystem.cpp @@ -802,11 +802,29 @@ static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params) char *buffer; pContext->LocalToString(params[2], &buffer); + if (params[4] != -1) + { + if (size_t(params[4]) > size_t(params[3])) + { + return pContext->ThrowNativeError("read_count (%u) is greater than buffer size (%u)", + params[4], + params[3]); + } + + num_read = (cell_t)fread(buffer, 1, params[4], pFile); + + if (num_read != params[4] && ferror(pFile)) + { + return -1; + } + + return num_read; + } + char val; while (1) { - /* If we're in stop mode, break as soon as the buffer is full. */ - if (params[4] && (params[3] == 0 || num_read >= params[3] - 1)) + if (params[3] == 0 || num_read >= params[3] - 1) { break; } diff --git a/plugins/include/files.inc b/plugins/include/files.inc index 65fb01fd..ab9eaf70 100644 --- a/plugins/include/files.inc +++ b/plugins/include/files.inc @@ -176,11 +176,11 @@ native ReadFile(Handle:hndl, items[], num_items, size); * @param hndl Handle to the file. * @param buffer Buffer to store the string. * @param max_size Maximum size of the string buffer. - * @param stop If true, reading will stop once max_size-1 bytes have - * been read. If false, reading will stop once a NUL - * terminator is reached. The buffer will simply be - * terminated in either case, the difference is in how - * the far the file position is changed. + * @param read_count If -1, reads until a null terminator is encountered in + * the file. Otherwise, read_count bytes are read + * into the buffer provided. In this case the buffer + * is not explicitly null terminated, and the buffer + * will contain any null terminators read from the file. * @return Number of characters written to the buffer, or -1 * if an error was encountered. * @error Invalid Handle, or read_count > max_size.