Ignore:
Timestamp:
2018-01-16T20:38:46Z (6 years ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2467b41
Parents:
d39c46e0
Message:

Remove const qualifier from the argument of free() and realloc(),
as well as in numerous other variables that hold ownership of memory.

By convention, a pointer that holds ownership is _never_ qualified by const.
This is reflected in the standard type signature of free() and realloc().
Allowing const pointers to hold ownership may seem superficially convenient,
but is actually quite confusing to experienced C programmers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/posix/source/stdio/scanf.c

    rd39c46e0 r33b8d024  
    625625
    626626                                const char *cur_borrowed = NULL;
     627                                char *cur_duplicated = NULL;
    627628                                const char *cur_limited = NULL;
    628                                 char *cur_updated = NULL;
     629                                const char *cur_updated = NULL;
    629630
    630631                                /* Borrow the cursor. Until it is returned to the provider
     
    637638                                 * than allowed by width. */
    638639                                if (width != -1) {
    639                                         cur_limited = posix_strndup(cur_borrowed, width);
     640                                        cur_duplicated = posix_strndup(cur_borrowed, width);
     641                                        cur_limited = cur_duplicated;
    640642                                } else {
    641643                                        cur_limited = cur_borrowed;
    642644                                }
    643                                 cur_updated = (char *) cur_limited;
     645                                cur_updated = cur_limited;
    644646
    645647                                long long sres = 0;
     
    648650                                /* Try to convert the integer. */
    649651                                if (int_conv_unsigned) {
    650                                         ures = strtoull(cur_limited, &cur_updated, int_conv_base);
     652                                        ures = strtoull(cur_limited, (char **) &cur_updated, int_conv_base);
    651653                                } else {
    652                                         sres = strtoll(cur_limited, &cur_updated, int_conv_base);
     654                                        sres = strtoll(cur_limited, (char **) &cur_updated, int_conv_base);
    653655                                }
    654656
    655657                                /* Update the cursor so it can be returned to the provider. */
    656658                                cur_borrowed += cur_updated - cur_limited;
    657                                 if (width != -1 && cur_limited != NULL) {
     659                                if (cur_duplicated != NULL) {
    658660                                        /* Deallocate duplicated part of the cursor view. */
    659                                         free(cur_limited);
     661                                        free(cur_duplicated);
    660662                                }
    661663                                cur_limited = NULL;
    662664                                cur_updated = NULL;
     665                                cur_duplicated = NULL;
    663666                                /* Return the cursor to the provider. Input consistency is again
    664667                                 * the job of the provider, so we can report errors from
     
    797800                                const char *cur_borrowed = NULL;
    798801                                const char *cur_limited = NULL;
    799                                 char *cur_updated = NULL;
     802                                char *cur_duplicated = NULL;
     803                                const char *cur_updated = NULL;
    800804
    801805                                /* Borrow the cursor. Until it is returned to the provider
     
    808812                                 * than allowed by width. */
    809813                                if (width != -1) {
    810                                         cur_limited = posix_strndup(cur_borrowed, width);
     814                                        cur_duplicated = posix_strndup(cur_borrowed, width);
     815                                        cur_limited = cur_duplicated;
    811816                                } else {
    812817                                        cur_limited = cur_borrowed;
    813818                                }
    814                                 cur_updated = (char *) cur_limited;
     819                                cur_updated = cur_limited;
    815820
    816821                                float fres = 0.0;
     
    821826                                switch (length_mod) {
    822827                                case LMOD_NONE:
    823                                         fres = posix_strtof(cur_limited, &cur_updated);
     828                                        fres = posix_strtof(cur_limited, (char **) &cur_updated);
    824829                                        break;
    825830                                case LMOD_l:
    826                                         dres = posix_strtod(cur_limited, &cur_updated);
     831                                        dres = posix_strtod(cur_limited, (char **) &cur_updated);
    827832                                        break;
    828833                                case LMOD_L:
    829                                         ldres = posix_strtold(cur_limited, &cur_updated);
     834                                        ldres = posix_strtold(cur_limited, (char **) &cur_updated);
    830835                                        break;
    831836                                default:
     
    835840                                /* Update the cursor so it can be returned to the provider. */
    836841                                cur_borrowed += cur_updated - cur_limited;
    837                                 if (width != -1 && cur_limited != NULL) {
     842                                if (cur_duplicated != NULL) {
    838843                                        /* Deallocate duplicated part of the cursor view. */
    839                                         free(cur_limited);
     844                                        free(cur_duplicated);
    840845                                }
    841846                                cur_limited = NULL;
Note: See TracChangeset for help on using the changeset viewer.