Skip to content

Conversation

@danielclow
Copy link
Contributor

@danielclow danielclow commented Dec 8, 2025

copy udf example code from tests into docs/examples for use in udf developer doc

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @danielclow, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the documentation for User-Defined Functions (UDFs) by centralizing all associated example code files into a new docs/examples/udf directory. This change aims to improve the organization and maintainability of UDF examples within the documentation, ensuring that the documentation directly includes the code rather than linking to external test scripts. The PR description indicates this is a test and should not be merged.

Highlights

  • Documentation Structure Update: The paths for User-Defined Function (UDF) example code inclusions in docs/en/07-develop/09-udf.md have been updated to reflect a new directory structure.
  • UDF Example Relocation: All C and Python UDF example source files have been moved from the tests/script/sh/ directory to a new, dedicated documentation examples directory: docs/examples/udf/.
  • External Link Removal: Direct external links to the GitHub repository for UDF examples have been removed from the documentation, now relying on the included code snippets.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@taosdata-bot taosdata-bot bot added the internal label Dec 8, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request moves UDF example files to a new docs/examples directory and updates the documentation accordingly. The changes are mostly correct, but I've found several issues in the example code files, including critical security vulnerabilities like buffer overflows, memory leaks, and logic bugs. I've also found some copy-paste errors and other mistakes in the documentation markdown file. My review includes suggestions to fix these issues.

Comment on lines 99 to 104
char *des = resultData->buf + sizeof(uint16_t);
strcpy(des, str);

// set binary type len
uint16_t len = strlen(str);
*((uint16_t *)resultData->buf) = len;
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

Using strcpy here is unsafe and can lead to a buffer overflow. The destination buffer size is limited by the outputtype in CREATE FUNCTION (e.g., 64 bytes), but the source string can be up to 256 bytes. You must use a bounded copy function like memcpy and respect the destination buffer's capacity.

Comment on lines 67 to 69
char data[64];
memset(data, 0, 64);
memcpy(data, varDataVal(buf), varDataLen(buf));
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This code has a critical buffer overflow vulnerability. A fixed-size buffer data[64] is used, but the varchar data from varDataLen(buf) can be much larger, leading to a buffer overflow in memcpy. You should dynamically allocate memory for data based on the actual length of the string.

Remember to free(data) at the end of the for loop iteration to prevent a memory leak.

udfTrace("block:%p, processing begins, cols:%d rows:%d", block, block->numOfCols, block->numOfRows);

float maxValue = *(float *)interBuf->buf;
char strBuff[STR_MAX_LEN] = "inter1buf";
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The strBuff is always initialized to the hardcoded string "inter1buf". This is a bug, as it discards the intermediate string result from previous blocks if no new maximum is found in the current block. strBuff should be initialized from the content of interBuf.

  char  strBuff[STR_MAX_LEN];
  memcpy(strBuff, interBuf->buf + sizeof(float), STR_MAX_LEN);

Comment on lines 25 to 28
if len(row) > 1:
new_state = np.cumsum(row)[-1]
else:
new_state = state
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The logic for calculating the cumulative sum is incorrect. It doesn't accumulate the sum from the previous state (state). Instead, it overwrites new_state with the sum of only the current block's values, and only if the block contains more than one value. This means the function doesn't work as a cumulative sum across blocks and ignores data in blocks with one or zero non-null values.

The logic should add the sum of the current block's values to the existing state.

    new_state = state + sum(row)

int32_t deviceIdLen = valDataLen < (STR_MAX_LEN - 1) ? valDataLen : (STR_MAX_LEN - 1);

strncpy(strBuff, deviceId, deviceIdLen);
snprintf(strBuff + deviceIdLen, STR_MAX_LEN - deviceIdLen, "_(%d,%d)_%f", j, i, maxValue);
Copy link
Contributor

Choose a reason for hiding this comment

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

high

There is a risk of string truncation here. If deviceIdLen is close to STR_MAX_LEN, the buffer size for snprintf (STR_MAX_LEN - deviceIdLen) will be too small to hold the formatted string "_(%d,%d)_%f". This will result in a truncated and incorrect output string. You should ensure deviceIdLen is limited to leave enough space for the suffix.


udfTrace("start aggregation, buflen:%d used:%d", buf->bufLen, bufLen);
memset(buf->buf, 0, sizeof(float) + STR_MAX_LEN);
*((float *)buf->buf) = INT32_MIN;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Initializing a float with INT32_MIN is not ideal. For correctness and portability, you should use a floating-point constant like -FLT_MAX from <float.h>. You will also need to include <float.h>.

  *((float *)buf->buf) = -FLT_MAX;

}

double sumSquares = *(double*)interBuf->buf;
int8_t numNotNull = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The variable numNotNull is initialized here and incremented on line 66, but its value is never used. It should be removed to avoid confusion and improve code clarity. The increment on line 66 should also be removed.

@tomchon tomchon requested a review from feici02 as a code owner December 8, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants