Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Code Reviews A simple implementation of a mutable String in C

(User JS1 answered as follows; source.) Unsafe loop This loop in string_find() is unsafe since it could read past the end of your buffer: while (str->data[pos] != c) ++pos; You shoul...

posted 8d ago by aura-lsprog-86‭  ·  edited 8d ago by aura-lsprog-86‭

Answer
#2: Post edited by user avatar aura-lsprog-86‭ · 2025-02-14T02:29:27Z (8 days ago)
Amend lack of MathJax in answer
  • _(User [JS1](https://codereview.stackexchange.com/users/58193/js1) answered as follows; [source](https://codereview.stackexchange.com/a/156351/98306).)_
  • <hr />
  • ## Unsafe loop
  • This loop in `string_find()` is unsafe since it could read past the end of your buffer:
  • > while (str->data[pos] != c) ++pos;
  • You should add an additional check like this:
  • while (pos < str->length && str->data[pos] != c) ++pos;
  • ## Short circuit error conditions
  • Rather than doing this:
  • > if (str != NULL) {
  • > // Rest of function indented
  • > }
  • it would be easier to read if you rewrote it like this:
  • if (str == NULL)
  • return NULL;
  • // Rest of function, not indented
  • ## Other things
  • Your reallocation strategy will lead to $O(n^2)$ behavior when appending to long strings. You might want to double the allocation instead of adding a fixed amount.
  • You might want to use `size_t` for your sizes and lengths instead if `int`, because an `int` might overflow at 32KB on some platforms.
  • _(User [JS1](https://codereview.stackexchange.com/users/58193/js1) answered as follows; [source](https://codereview.stackexchange.com/a/156351/98306).)_
  • <hr />
  • ## Unsafe loop
  • This loop in `string_find()` is unsafe since it could read past the end of your buffer:
  • > while (str->data[pos] != c) ++pos;
  • You should add an additional check like this:
  • while (pos < str->length && str->data[pos] != c) ++pos;
  • ## Short circuit error conditions
  • Rather than doing this:
  • > if (str != NULL) {
  • > // Rest of function indented
  • > }
  • it would be easier to read if you rewrote it like this:
  • if (str == NULL)
  • return NULL;
  • // Rest of function, not indented
  • ## Other things
  • Your reallocation strategy will lead to **O(n^2)** behavior when appending to long strings. You might want to double the allocation instead of adding a fixed amount.
  • You might want to use `size_t` for your sizes and lengths instead if `int`, because an `int` might overflow at 32KB on some platforms.
#1: Initial revision by user avatar aura-lsprog-86‭ · 2025-02-14T02:27:19Z (8 days ago)
_(User [JS1](https://codereview.stackexchange.com/users/58193/js1) answered as follows; [source](https://codereview.stackexchange.com/a/156351/98306).)_

<hr />

## Unsafe loop

This loop in `string_find()` is unsafe since it could read past the end of your buffer:

>     while (str->data[pos] != c) ++pos;

You should add an additional check like this:

    while (pos < str->length && str->data[pos] != c) ++pos;

## Short circuit error conditions

Rather than doing this:

>     if (str != NULL) {
>         // Rest of function indented
>     }

it would be easier to read if you rewrote it like this:

    if (str == NULL)
        return NULL;

    // Rest of function, not indented

## Other things

Your reallocation strategy will lead to $O(n^2)$ behavior when appending to long strings. You might want to double the allocation instead of adding a fixed amount.

You might want to use `size_t` for your sizes and lengths instead if `int`, because an `int` might overflow at 32KB on some platforms.