patterncModerate
Python-like C string library
Viewed 0 times
likestringpythonlibrary
Problem
Most of the C code I have written has never been seen by anybody else, and I wonder if my code follows normal practices, especially how my code performs security-wise. The code is for a library that I have made for working with strings, and it is for use in other programs.
The full project can be found on GitHub.
The following is a snippet of the
```
/**
* @brief This function handels the cases for pl_translate where there is no
* table. It should not be called directly, call pl_translate with the table
* parameter set as NULL instead.
*/
char translate_no_table(char string, char *deletechars) {
char tmp = NULL, ret_val = NULL;
int i, x, found = 0, idx = 0;
if (string == NULL || deletechars == NULL) {
goto error_exit;
}
if (strlen(string) == 0 || strlen(deletechars) == 0) {
goto error_exit;
}
for (i = 0; i 'read this short text', and deletechars is \a 'aeiou', the returned
* string is 'rd ths shrt txt'.
*
* If the table parameter is not empty, every occurrence of one of the table
* characters is replaced with the character in deletechars at the same index.
* So if the same string is passed 'read this short text', and the table
* is \a 'xxxxx', and deletechars is \a 'aeiou' the returned string is
* 'rxxd thxs shxrt txxt'. The length of the table and deletechars needs
* to be of the same length, or else the function will return \b NULL.
*
* You need to free the returned buffer after use.
*
* @param string The string you want to translate.
*
* @param table Optional parameter, if set it is used to swap the characters
* passed in the deletechars parameter. If not every occurrence of the characters
* passed in deletechars is removed from the string.
*
* @param deletechars The characters in the parameter is removed or swapped out
* from the string. If the table parameter is used this parameter and table
* needs to be of equal size.
*
* @r
The full project can be found on GitHub.
The following is a snippet of the
plstr.c file, the example usage comments were removed:```
/**
* @brief This function handels the cases for pl_translate where there is no
* table. It should not be called directly, call pl_translate with the table
* parameter set as NULL instead.
*/
char translate_no_table(char string, char *deletechars) {
char tmp = NULL, ret_val = NULL;
int i, x, found = 0, idx = 0;
if (string == NULL || deletechars == NULL) {
goto error_exit;
}
if (strlen(string) == 0 || strlen(deletechars) == 0) {
goto error_exit;
}
for (i = 0; i 'read this short text', and deletechars is \a 'aeiou', the returned
* string is 'rd ths shrt txt'.
*
* If the table parameter is not empty, every occurrence of one of the table
* characters is replaced with the character in deletechars at the same index.
* So if the same string is passed 'read this short text', and the table
* is \a 'xxxxx', and deletechars is \a 'aeiou' the returned string is
* 'rxxd thxs shxrt txxt'. The length of the table and deletechars needs
* to be of the same length, or else the function will return \b NULL.
*
* You need to free the returned buffer after use.
*
* @param string The string you want to translate.
*
* @param table Optional parameter, if set it is used to swap the characters
* passed in the deletechars parameter. If not every occurrence of the characters
* passed in deletechars is removed from the string.
*
* @param deletechars The characters in the parameter is removed or swapped out
* from the string. If the table parameter is used this parameter and table
* needs to be of equal size.
*
* @r
Solution
-
Standard doesn't specify the signedness of char. The line
fails on a default-signed-char architecture (
-
Functions which
should not be exposed to the client. Make them
-
-
Don't use magic numbers (9, 10, 13). They are
-
Standard doesn't specify the signedness of char. The line
swap_table[(int) table[i]] = deletechars[i];fails on a default-signed-char architecture (
table[i] is promoted to negative value). Make it unsigned char * table.-
Functions which
should not be called directlyshould not be exposed to the client. Make them
static.-
translate_no_table- Counting
foundin is a waste of time. A resulting string is at most as large as the original.
translate_no_tableshould also use a lookup (liketranslate_with_tabledoes ) table made from from deletechars. That would reduce complexity from NM to N+M (N, M being lengths ofstringanddeletechars).
-
Don't use magic numbers (9, 10, 13). They are
'\t', '\r', '\n' really.-
pl_expandtabs has a semantical problem. It replaces the tab character with a given number of spaces. It is not what is usually expected. Instead, the number of spaces shall be calculated in such way that the character following the tab appears at a column which is multiple of tab width.Code Snippets
swap_table[(int) table[i]] = deletechars[i];should not be called directlyContext
StackExchange Code Review Q#84377, answer score: 10
Revisions (0)
No revisions yet.