Conversation
|
I cannot accept this pull request. Although your code clearly looks easier, it seems to be not correct, as the strings in question in Maybe |
|
Thanks for your deeper look.
I can not understand your concern, because AFAIK Additionally I'm curious, why you removed the
Anyway I think, it already does this detection by: But if all string fields in Additionally I think, the code in And I suspect if the whole function may be superfluous, if |
…jpilot into glob_prefs_init_stripped
|
Here is an example #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[]) {
char *s = "ABC";
char *t = strdup("UVW");
char *sr = realloc(s,1024);
char *tr = realloc(t,1024);
printf("s=%lx, t=%lx, sr=%lx, tr=%lx\n",
(long)s,(long)t,(long)sr,(long)tr);
return 0;
}Compiling this code with realloctest.c: In function ‘main’:
realloctest.c:10:9: warning: pointer ‘s’ used after ‘realloc’ [-Wuse-after-free]
10 | printf("s=%lx, t=%lx, sr=%lx, tr=%lx\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 | (long)s,(long)t,(long)sr,(long)tr);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
realloctest.c:8:20: note: call to ‘realloc’ here
8 | char *sr = realloc(s,1024);
| ^~~~~~~~~~~~~~~
realloctest.c:10:9: warning: pointer ‘t’ used after ‘realloc’ [-Wuse-after-free]
10 | printf("s=%lx, t=%lx, sr=%lx, tr=%lx\n",
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 | (long)s,(long)t,(long)sr,(long)tr);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
realloctest.c:9:20: note: call to ‘realloc’ here
9 | char *tr = realloc(t,1024);
| ^~~~~~~~~~~~~~~
realloctest.c:8:20: warning: ‘realloc’ called on a pointer to an unallocated object ‘"ABC"’ [-Wfree-nonheap-object]
8 | char *sr = realloc(s,1024);
| ^~~~~~~~~~~~~~~
realloctest.c:6:15: note: assigned here
6 | char *s = "ABC";
| ^Syntax is correct. Code looks "clean". The problematic part here is: ‘realloc’ called on a pointer to an unallocated object ‘. If you run the program: ./realloctest
zsh: segmentation fault (core dumped) ./realloctestThat's the best what can happen ;-) |
Correct. The problem happens, because your sample code copies the new pointer from But in On the other hand, even with the original code you can provoke the problem with: This is guaranteed not to happen in the JPilot code, as you can see in I too have compiled my code with |
I don't understand why the strings of the
glob_prefsare initialised with the default values using the longswitch ... casestatement (from line 172) with the default values instead of using the compactjp_pref_init(...)function. In addition, it looks much more organised and clearer if strings are defined directly in the definition of theglob_prefsarray.It should also run faster as it saves from layawaying through several switch cases.
It may also help to avoid issue #53 , because several
svaluefields ofglob_prefsstayNULLinstead of being initialized to""andsvalue_sizefields stay0instead1. While developing my JPilotMediaPlugin (see from line 68) I often have seen suchSIGCHLDcrashes while reading or writing the prefs before I properly initialized (line 965) those fields.And last, but not least, since
strdup()allocates heap memory, isn't it also a bug that it is not freed anywhere, which is what thejp_free_prefs()function was intended for. This may also help to avoid issue #53 .