Coverity fixes from Nelson
authorSteve Youngs <steve@sxemacs.org>
Fri, 16 Sep 2011 09:17:22 +0000 (19:17 +1000)
committerSteve Youngs <steve@sxemacs.org>
Fri, 16 Sep 2011 09:17:22 +0000 (19:17 +1000)
* njsf-merges:
  Fix stupid strncat usage mistake Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure a pointer to the local ures is not returned, but a copy instead. CID:199 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure to fclose the file on error CID:195 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure to use add.*_rune_noret when not interested on the return value.. CID:175,177,190 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure that the args array is initialized CID:394 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure dl.top_clip is initialized CID:406 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Avoid doing decode_buffer twice when not doing clash detection CID:430 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Remove unneeded initialization of command_builder pointer. CID:431 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Do not assign the return of setlocale(LC_ALL,"C") to locale var since it will be no longer used afterwards. CID:432 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Remove unused value of eckey CID:435 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure that check and change of state is done on the mutual exclusion zone. move the sleep outside of it. CID:440
  make sure the optable_index cannot be negative, instead an assertion is made. CID:117,116,115,114,111,110,109,88,87 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure avfc is not NULL before dereferencing CID:457 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure that avfc is non null before de-reference. CID:458 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure the pointers are valid before dereferencing CID:461 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Use strncat instead of strcat, even in a paranoid fashion. CID:463 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>
  Make sure that mtp has the proper play state before entering the loop. CID:466 Signed-off-by: Nelson Ferreira <nelson.ferreira@ieee.org>

modules/ase/ase-interval.c
src/buffer.c
src/ent/ent.c
src/events/event-stream.c
src/media/media-ffmpeg.c
src/media/sound-ao.c
src/mule/input-method-xlib.c
src/openssl.c
src/ui/X11/toolbar-x.c
src/ui/redisplay.c

index 777fdff..f8e2c71 100644 (file)
@@ -2044,6 +2044,12 @@ _ase_subtract_intr_union(ase_cartesian_t c, ase_interval_union_item_t u)
                u = u->next;
        }
 
+       if (na == &ures) {
+               /* Copy the local temporary to the heap */
+               na = xnew(struct ase_interval_union_item_s);
+               assert(na);
+               memcpy(na,&ures,sizeof(ures));
+       }
        return na;
 }
 
@@ -3042,23 +3048,26 @@ static Lisp_Object
 __ase_interval_union_rational(ase_interval_union_item_t u)
 {
        int i = 0, nargs = __ase_interval_union_update_rational(u);
-       Lisp_Object args[nargs];
-
        if (nargs == 0)
                return Qzero;
-
-       while (u) {
-               if (ASE_INTERVALP(u->current)) {
-                       args[i] = _ase_interval_rational(
-                               XASE_INTERVAL(u->current));
-               } else if (ASE_INTERVAL_INTERIOR_P(u->current)) {
-                       args[i] = _ase_interval_interior_rational(
-                               XASE_CARTESIAN(u->current));
+       {
+               Lisp_Object args[nargs];
+               for ( i = nargs; i > 0; )
+                       args[--i] = Qnil;
+
+               while (u) {
+                       if (ASE_INTERVALP(u->current)) {
+                               args[i] = _ase_interval_rational(
+                                       XASE_INTERVAL(u->current));
+                       } else if (ASE_INTERVAL_INTERIOR_P(u->current)) {
+                               args[i] = _ase_interval_interior_rational(
+                                       XASE_CARTESIAN(u->current));
+                       }
+                       i++;
+                       u = u->next;
                }
-               i++;
-               u = u->next;
+               return ent_binop_many(ASE_BINARY_OP_SUM, nargs, args);
        }
-       return ent_binop_many(ASE_BINARY_OP_SUM, nargs, args);
 }
 
 static inline void
index 63a3a7a..b21dc71 100644 (file)
@@ -839,12 +839,12 @@ as BUFFER means use current buffer.
       (flag, buffer))
 {
        /* This function can GC */
-       struct buffer *buf = decode_buffer(buffer, 0);
+       struct buffer *buf;
 
 #ifdef CLASH_DETECTION
        /* If buffer becoming modified, lock the file.
           If buffer becoming unmodified, unlock the file.  */
-
+       buf =  decode_buffer(buffer, 0);
        Lisp_Object fn = buf->file_truename;
        if (!NILP(fn)) {
                int already = BUF_SAVE_MODIFF(buf) < BUF_MODIFF(buf);
index eb9e407..d9df318 100644 (file)
@@ -498,10 +498,8 @@ int
 ase_optable_index_typesym(Lisp_Object typesym)
 {
        Lisp_Object idx = Fget(typesym, Qoptable_index, Qnil);
-       if (INTEGERP(idx))
-               return XINT(idx);
-       else
-               return -1;
+       assert(INTEGERP(idx));
+       return XINT(idx);
 }
 
 /* categorial subtleties */
index 6c098a3..f5c0c13 100644 (file)
@@ -4398,8 +4398,7 @@ See `function-key-map' for more details.
                                                                   Probably not -- see
                                                                   comment in
                                                                   next-event */
-       struct command_builder *command_builder =
-           XCOMMAND_BUILDER(con->command_builder);
+       struct command_builder *command_builder;
        Lisp_Object result;
        Lisp_Object event = Fmake_event(Qnil, Qnil);
        int speccount = specpdl_depth();
index ff3ef6b..dc1bcef 100644 (file)
@@ -135,29 +135,32 @@ char *media_ffmpeg_streaminfo(Lisp_Media_Stream *ms)
        avfc = media_stream_data(ms);
        out = xmalloc_atomic(chars_left+1);
        out[0] = '\0';
+       out[chars_left] = '\0';
 
        /* cannot use ffmpeg on corrupt streams */
        if (media_stream_driver(ms) != MYSELF || avfc == NULL)
                return out;
 
        if (avfc->author && *avfc->author) {
-               strcat(out, " :author \"");
+               strncat(out, " :author \"", chars_left);
+               chars_left -= 10;
                strncat(out, avfc->author, chars_left);
-               strcat(out, "\"");
-               chars_left -= 560;
+               chars_left -= strlen(avfc->author);
+               strncat(out, "\"", chars_left--);
        }
        if (avfc->title && *avfc->title) {
-               strcat(out, " :title: \"");
+               strncat(out, " :title: \"", chars_left);
+               chars_left -= 10;
                strncat(out, avfc->title, chars_left);
-               strcat(out, "\"");
-               chars_left -= 560;
+               chars_left -= strlen(avfc->title);
+               strncat(out, "\"", chars_left--);
        }
        if (avfc->year) {
                char year[12];
-               strcat(out, " :year ");
+               strncat(out, " :year ", chars_left);
+               chars_left -= 7;
                snprintf(year, 12, "%d", avfc->year);
                strncat(out, year, chars_left);
-               chars_left -= 24;
        }
 
        return out;
@@ -379,12 +382,14 @@ media_ffmpeg_analyse_audio(media_substream *mss, AVFormatContext *avfc, int st)
        const char *name = NULL;
        const char *codec_name = NULL;
        /* libavformat cruft */
-       AVStream *avst;
-       AVCodecContext *avcc;
+       AVStream *avst = NULL;
+       AVCodecContext *avcc = NULL;
 
        /* unpack the stream and codec context from the container, again */
-       avst = avfc->streams[st];
-       avcc = avst->codec;
+       if (avfc)
+         avst = avfc->streams[st];
+       if (avst)
+         avcc = avst->codec;
 
        /* initialise */
        mtap = xnew_and_zero(mtype_audio_properties);
@@ -397,9 +402,11 @@ media_ffmpeg_analyse_audio(media_substream *mss, AVFormatContext *avfc, int st)
 
        mtap->name = name;
        mtap->codec_name = codec_name;
-       mtap->channels = avcc->channels;
-       mtap->samplerate = avcc->sample_rate;
-       mtap->bitrate = media_ffmpeg_bitrate(avcc);
+       if (avcc ) {
+               mtap->channels = avcc->channels;
+               mtap->samplerate = avcc->sample_rate;
+               mtap->bitrate = media_ffmpeg_bitrate(avcc);
+       }
 
        /* samplewidth and framesize */
        switch (avcc->sample_fmt) {
@@ -449,12 +456,14 @@ media_ffmpeg_analyse_video(media_substream *mss, AVFormatContext *avfc, int st)
        const char *name = NULL;
        const char *codec_name = NULL;
        /* libavformat cruft */
-       AVStream *avst;
-       AVCodecContext *avcc;
+       AVStream *avst = NULL;
+       AVCodecContext *avcc = NULL;
 
        /* unpack the stream and codec context from the container, again */
-       avst = avfc->streams[st];
-       avcc = avst->codec;
+       if (avfc)
+         avst = avfc->streams[st];
+       if (avst)
+         avcc = avst->codec;
 
        /* initialise */
        mtvp = xnew_and_zero(mtype_video_properties);
@@ -467,11 +476,13 @@ media_ffmpeg_analyse_video(media_substream *mss, AVFormatContext *avfc, int st)
 
        mtvp->name = name;
        mtvp->codec_name = codec_name;
-       mtvp->bitrate = avcc->bit_rate;
-       mtvp->width = avcc->width;
-       mtvp->height = avcc->height;
-       mtvp->aspect_num = avcc->sample_aspect_ratio.num;
-       mtvp->aspect_den = avcc->sample_aspect_ratio.den;
+       if (avcc) {
+         mtvp->bitrate = avcc->bit_rate;
+         mtvp->width = avcc->width;
+         mtvp->height = avcc->height;
+         mtvp->aspect_num = avcc->sample_aspect_ratio.num;
+         mtvp->aspect_den = avcc->sample_aspect_ratio.den;
+       }
 
        mtvp->endianness = 0;
 
@@ -542,42 +553,43 @@ media_ffmpeg_open(Lisp_Media_Stream *ms)
                break;
        }
 
-       /* check if there is at least one usable stream */
-       for (size_t st = 0; st < avfc->nb_streams; st++) {
-               avst = avfc->streams[st];
-               avcc = avst->codec;
-               if (avcc &&
-                   avcc->codec_id != CODEC_ID_NONE &&
-                   avcc->codec_type != CODEC_TYPE_DATA &&
-                   (avc = avcodec_find_decoder(avcc->codec_id)) &&
-                   (avc && (avcodec_open(avcc, avc) >= 0))) {
-
-                       /* create a substream */
-                       mss = make_media_substream_append(ms);
-
-                       switch ((unsigned int)avcc->codec_type) {
-                       case CODEC_TYPE_VIDEO:
-                               /* assign substream props */
-                               media_substream_type(mss) = MTYPE_VIDEO;
-                               media_ffmpeg_analyse_video(mss, avfc, st);
-                               break;
-                       case CODEC_TYPE_AUDIO:
-                               /* assign substream props */
-                               media_substream_type(mss) = MTYPE_AUDIO;
-                               media_ffmpeg_analyse_audio(mss, avfc, st);
-                               /* set some stream handlers */
-                               media_stream_set_meths(ms, media_ffmpeg);
-                               break;
-                       case CODEC_TYPE_DATA:
-                               media_substream_type(mss) = MTYPE_IMAGE;
-                               break;
-                       default:
-                               media_substream_type(mss) = MTYPE_UNKNOWN;
-                               break;
+       if (avfc)
+               /* check if there is at least one usable stream */
+               for (size_t st = 0; st < avfc->nb_streams; st++) {
+                       avst = avfc->streams[st];
+                       avcc = avst->codec;
+                       if (avcc &&
+                           avcc->codec_id != CODEC_ID_NONE &&
+                           avcc->codec_type != CODEC_TYPE_DATA &&
+                           (avc = avcodec_find_decoder(avcc->codec_id)) &&
+                           (avc && (avcodec_open(avcc, avc) >= 0))) {
+                               
+                               /* create a substream */
+                               mss = make_media_substream_append(ms);
+                               
+                               switch ((unsigned int)avcc->codec_type) {
+                               case CODEC_TYPE_VIDEO:
+                                       /* assign substream props */
+                                       media_substream_type(mss) = MTYPE_VIDEO;
+                                       media_ffmpeg_analyse_video(mss, avfc, st);
+                                       break;
+                               case CODEC_TYPE_AUDIO:
+                                       /* assign substream props */
+                                       media_substream_type(mss) = MTYPE_AUDIO;
+                                       media_ffmpeg_analyse_audio(mss, avfc, st);
+                                       /* set some stream handlers */
+                                       media_stream_set_meths(ms, media_ffmpeg);
+                                       break;
+                               case CODEC_TYPE_DATA:
+                                       media_substream_type(mss) = MTYPE_IMAGE;
+                                       break;
+                               default:
+                                       media_substream_type(mss) = MTYPE_UNKNOWN;
+                                       break;
+                               }
                        }
                }
-       }
-
+       
        /* keep the format context */
        media_stream_data(ms) = avfc;
 
index 4b05f31..9c5c1f4 100644 (file)
@@ -356,6 +356,7 @@ sound_ao_play(audio_job_t aj)
 
        /* ... and play it */
        SXE_MUTEX_LOCK(&aj->mtx);
+       mtp = aj->play_state;
        if (aj->buffer_alloc_size < SOUND_MAX_AUDIO_FRAME_SIZE) {
                alloced_myself = 1;
                aj->buffer = xmalloc_atomic(SOUND_MAX_AUDIO_FRAME_SIZE);
@@ -374,29 +375,27 @@ sound_ao_play(audio_job_t aj)
 #endif
 
                SXE_MUTEX_LOCK(&aj->mtx);
-               mtp = aj->play_state;
-               SXE_MUTEX_UNLOCK(&aj->mtx);
-               switch (mtp) {
+               switch (aj->play_state) {
                case MTPSTATE_RUN:
-                       if (!ao_push(aj, resolution)) {
-                               SXE_MUTEX_LOCK(&aj->mtx);
-                               mtp = aj->play_state = MTPSTATE_STOP;
-                               SXE_MUTEX_UNLOCK(&aj->mtx);
-                       }
+                       if (!ao_push(aj, resolution))
+                               aj->play_state = MTPSTATE_STOP;
                        break;
                case MTPSTATE_PAUSE:
-                       AO_DEBUG("sleeping for %d\n", resolution);
-                       usleep(resolution);
-                       break;
+                 /* must sleep resolution outside of lock */
 
                case MTPSTATE_UNKNOWN:
                case MTPSTATE_STOP:
                case NUMBER_OF_MEDIA_THREAD_PLAY_STATES:
                default:
                        AO_DEBUG("ACK, quit\n");
-                       SXE_MUTEX_LOCK(&aj->mtx);
-                       mtp = aj->play_state = MTPSTATE_STOP;
-                       SXE_MUTEX_UNLOCK(&aj->mtx);
+                       aj->play_state = MTPSTATE_STOP;
+                       break;
+               }
+               mtp = aj->play_state
+               SXE_MUTEX_UNLOCK(&aj->mtx);
+               if (mtp == MTPSTATE_PAUSE) {
+                       AO_DEBUG("sleeping for %d\n", resolution);
+                       usleep(resolution);
                        break;
                }
        }
index cf20e90..eb083af 100644 (file)
@@ -158,7 +158,7 @@ Initialize_Locale (void)
                           "Using C Locale instead\n", locale);
                putenv ("LANG=C");
                putenv ("LC_ALL=C");
-               if ((locale = setlocale (LC_ALL, "C")) == NULL) {
+               if (setlocale (LC_ALL, "C") == NULL) {
                        xim_warn ("Can't set locale to `C'!\n");
                        return;
                }
index 93c014c..2698834 100644 (file)
@@ -876,9 +876,11 @@ binary string data.
        file = Fexpand_file_name(file, Qnil);
 
        if (((fp = fopen((char *)XSTRING_DATA(file),"rb")) == NULL) ||
-           (fseek(fp, 0, SEEK_SET)))
+           (fseek(fp, 0, SEEK_SET))) {
+               if (fp)
+                       fclose(fp);
                return wrong_type_argument(Qfile_readable_p, file);
-
+       }
 
        OpenSSL_add_all_digests();
        md = EVP_get_digestbyname(
@@ -886,6 +888,7 @@ binary string data.
 
        if (!md) {
                EVP_cleanup();
+               fclose(fp);
                error ("no such digest");
        }
 
@@ -2391,7 +2394,7 @@ At the moment we do not support creating custom curves.
       (curve))
 {
        EVP_PKEY *pkey;
-       EC_KEY *eckey = EC_KEY_new();
+       EC_KEY *eckey;
 
        CHECK_SYMBOL(curve);
 
@@ -2399,7 +2402,7 @@ At the moment we do not support creating custom curves.
        eckey = EC_KEY_new_by_curve_name(
                ec_curve_by_name((char *)string_data(XSYMBOL(curve)->name)));
 
-       if ((eckey == NULL)) {
+       if (eckey == NULL) {
                error ("no such curve");
        }
 
index 3f326f6..67eb9a1 100644 (file)
@@ -233,6 +233,7 @@ static void x_output_toolbar_button(struct frame *f, Lisp_Object button)
                        dl.ascent = fm.ascent;
                        dl.descent = fm.descent;
                        dl.ypos = tb->y + y_offset + fm.ascent;
+                       dl.top_clip = 0;
 
                        if (fm.ascent + fm.descent <= height) {
                                dl.ypos +=
index 1c21bac..905edf1 100644 (file)
@@ -2664,7 +2664,7 @@ create_text_block(struct window *w, struct display_line *dl,
                                cachel = GLYPH_CACHEL(w, CONT_GLYPH_INDEX);
                        }
 
-                       add_glyph_rune(&data, &gb, BEGIN_GLYPHS, 0, cachel);
+                       add_glyph_rune_noret(&data, &gb, BEGIN_GLYPHS, 0, cachel);
 
                        if (truncate_win && data.bi_bufpos == BI_BUF_ZV(b)
                            && BI_BUF_FETCH_CHAR(b,
@@ -3758,7 +3758,7 @@ add_string_to_fstring_db_runes(pos_data * data, const Bufbyte * str,
 
        data->blank_width = space_width(XWINDOW(data->window));
        while (Dynarr_length(db->runes) < pos)
-               add_blank_rune(data, NULL, 0);
+               add_blank_rune_noret(data, NULL, 0);
 
        end = (Dynarr_length(db->runes) +
               bytecount_to_charcount(str, strlen((const char *)str)));
@@ -3813,7 +3813,7 @@ add_glyph_to_fstring_db_runes(pos_data * data, Lisp_Object glyph,
 
        while (Dynarr_length(db->runes) < pos &&
               (data->pixpos + data->blank_width <= data->max_pixpos))
-               add_blank_rune(data, NULL, 0);
+               add_blank_rune_noret(data, NULL, 0);
 
        return Dynarr_length(db->runes);
 }